MythTV  master
avformatdecoder.cpp
Go to the documentation of this file.
1 // C++ headers
2 #include <algorithm>
3 #include <cassert>
4 #include <cmath>
5 #include <cstdint>
6 #include <iostream>
7 #include <unistd.h>
8 using namespace std;
9 
10 #include <QTextCodec>
11 #include <QFileInfo>
12 
13 // MythTV headers
14 #include "mythtvexp.h"
15 #include "mythconfig.h"
16 #include "avformatdecoder.h"
17 #include "privatedecoder.h"
18 #include "audiooutput.h"
19 #include "audiooutpututil.h"
20 #include "ringbuffer.h"
21 #include "mythplayer.h"
22 #include "remoteencoder.h"
23 #include "programinfo.h"
24 #include "mythcorecontext.h"
25 #include "mythdbcon.h"
26 #include "iso639.h"
27 #include "mpegtables.h"
28 #include "atscdescriptors.h"
29 #include "dvbdescriptors.h"
30 #include "cc608decoder.h"
31 #include "cc708decoder.h"
32 #include "teletextdecoder.h"
33 #include "subtitlereader.h"
34 #include "interactivetv.h"
35 #include "videodisplayprofile.h"
36 #include "mythuihelper.h"
37 #include "DVD/dvdringbuffer.h"
38 #include "Bluray/bdringbuffer.h"
39 #include "mythavutil.h"
40 
41 #include "lcddevice.h"
42 
43 #include "audiooutput.h"
44 #include "mythcodeccontext.h"
45 
46 #ifdef USING_VDPAU
47 #include "videoout_vdpau.h"
48 extern "C" {
49 #include "libavcodec/vdpau.h"
50 }
51 #endif // USING_VDPAU
52 
53 #ifdef USING_DXVA2
54 #include "videoout_d3d.h"
55 #endif
56 
57 #ifdef USING_GLVAAPI
58 #include "videoout_openglvaapi.h"
59 #endif // USING_GLVAAPI
60 #ifdef USING_VAAPI
61 #include "vaapicontext.h"
62 #endif
63 
64 #ifdef USING_MEDIACODEC
65 #include "mediacodeccontext.h"
66 extern "C" {
67 #include "libavcodec/jni.h"
68 }
69 #include <QtAndroidExtras>
70 #endif
71 
72 #ifdef USING_VAAPI2
73 #include "vaapi2context.h"
74 #endif
75 
76 extern "C" {
77 #include "libavutil/avutil.h"
78 #include "libavutil/error.h"
79 #include "libavutil/log.h"
80 #include "libavcodec/avcodec.h"
81 #include "libavformat/avformat.h"
82 #include "libavformat/avio.h"
83 #include "libavformat/internal.h"
84 #include "libswscale/swscale.h"
85 #include "libavformat/isom.h"
86 #include "ivtv_myth.h"
87 #include "libavutil/imgutils.h"
88 }
89 
90 #ifdef _MSC_VER
91 // MSVC isn't C99 compliant...
92 # ifdef AV_TIME_BASE_Q
93 # undef AV_TIME_BASE_Q
94 # endif
95 #define AV_TIME_BASE_Q GetAVTimeBaseQ()
96 
97 __inline AVRational GetAVTimeBaseQ()
98 {
99  AVRational av = {1, AV_TIME_BASE};
100  return av;
101 }
102 #endif
103 
104 #define LOC QString("AFD: ")
105 
106 // Maximum number of sequential invalid data packet errors
107 // before we try switching to software decoder
108 #define SEQ_PKT_ERR_MAX 10
109 
110 static const int max_video_queue_size = 220;
111 
112 static int cc608_parity(uint8_t byte);
113 static int cc608_good_parity(const int *parity_table, uint16_t data);
114 static void cc608_build_parity_table(int *parity_table);
115 
116 static bool silence_ffmpeg_logging = false;
117 
118 static QSize get_video_dim(const AVCodecContext &ctx)
119 {
120  return QSize(ctx.width >> ctx.lowres, ctx.height >> ctx.lowres);
121 }
122 static float get_aspect(const AVCodecContext &ctx)
123 {
124  float aspect_ratio = 0.0f;
125 
126  if (ctx.sample_aspect_ratio.num && ctx.height)
127  {
128  aspect_ratio = av_q2d(ctx.sample_aspect_ratio) *
129  static_cast<double>(ctx.width);
130  aspect_ratio /= (float) ctx.height;
131  }
132 
133  if (aspect_ratio <= 0.0f || aspect_ratio > 6.0f)
134  {
135  if (ctx.height)
136  aspect_ratio = (float)ctx.width / (float)ctx.height;
137  else
138  aspect_ratio = 4.0f / 3.0f;
139  }
140 
141  return aspect_ratio;
142 }
143 static float get_aspect(H264Parser &p)
144 {
145  static const float default_aspect = 4.0f / 3.0f;
146  int asp = p.aspectRatio();
147  switch (asp)
148  {
149  case 0: return default_aspect;
150  case 2: return 4.0f / 3.0f;
151  case 3: return 16.0f / 9.0f;
152  case 4: return 2.21f;
153  default: break;
154  }
155 
156  float aspect_ratio = asp * 0.000001f;
157  if (aspect_ratio <= 0.0f || aspect_ratio > 6.0f)
158  {
159  if (p.pictureHeight() && p.pictureWidth())
160  {
161  aspect_ratio =
162  (float) p.pictureWidth() /(float) p.pictureHeight();
163  }
164  else
165  {
166  aspect_ratio = default_aspect;
167  }
168  }
169  return aspect_ratio;
170 }
171 
172 
173 int get_avf_buffer(struct AVCodecContext *c, AVFrame *pic, int flase);
174 void release_avf_buffer(void *opaque, uint8_t *data);
175 #ifdef USING_VDPAU
176 int get_avf_buffer_vdpau(struct AVCodecContext *c, AVFrame *pic, int flags);
177 void release_avf_buffer_vdpau(void *opaque, uint8_t *data);
178 int render_wrapper_vdpau(struct AVCodecContext *s, AVFrame *src,
179  const VdpPictureInfo *info,
180  uint32_t count,
181  const VdpBitstreamBuffer *buffers);
182 #endif
183 #ifdef USING_DXVA2
184 int get_avf_buffer_dxva2(struct AVCodecContext *c, AVFrame *pic, int flags);
185 #endif
186 #ifdef USING_VAAPI
187 int get_avf_buffer_vaapi(struct AVCodecContext *c, AVFrame *pic, int flags);
188 #endif
189 #ifdef USING_VAAPI2
190 int get_avf_buffer_vaapi2(struct AVCodecContext *c, AVFrame *pic, int flags);
191 #endif
192 
193 static int determinable_frame_size(struct AVCodecContext *avctx)
194 {
195  if (/*avctx->codec_id == AV_CODEC_ID_AAC ||*/
196  avctx->codec_id == AV_CODEC_ID_MP1 ||
197  avctx->codec_id == AV_CODEC_ID_MP2 ||
198  avctx->codec_id == AV_CODEC_ID_MP3/* ||
199  avctx->codec_id == AV_CODEC_ID_CELT*/)
200  return 1;
201  return 0;
202 }
203 
204 static int has_codec_parameters(AVStream *st)
205 {
206  AVCodecContext *avctx = nullptr;
207 
208 #define FAIL(errmsg) do { \
209  LOG(VB_PLAYBACK, LOG_DEBUG, LOC + (errmsg)); \
210  return 0; \
211 } while (0)
212 
213  switch (st->codecpar->codec_type)
214  {
215  case AVMEDIA_TYPE_AUDIO:
216  avctx = gCodecMap->getCodecContext(st);
217  if (!avctx)
218  FAIL("No codec for audio stream");
219  if (!avctx->frame_size && determinable_frame_size(avctx))
220  FAIL("unspecified frame size");
221  if (avctx->sample_fmt == AV_SAMPLE_FMT_NONE)
222  FAIL("unspecified sample format");
223  if (!avctx->sample_rate)
224  FAIL("unspecified sample rate");
225  if (!avctx->channels)
226  FAIL("unspecified number of channels");
227  if (!st->nb_decoded_frames && avctx->codec_id == AV_CODEC_ID_DTS)
228  FAIL("no decodable DTS frames");
229  break;
230  case AVMEDIA_TYPE_VIDEO:
231  avctx = gCodecMap->getCodecContext(st);
232  if (!avctx)
233  FAIL("No codec for video stream");
234  if (!avctx->width)
235  FAIL("unspecified size");
236  if (avctx->pix_fmt == AV_PIX_FMT_NONE)
237  FAIL("unspecified pixel format");
238  if (avctx->codec_id == AV_CODEC_ID_RV30 || avctx->codec_id == AV_CODEC_ID_RV40)
239  if (!st->sample_aspect_ratio.num && !avctx->sample_aspect_ratio.num && !st->codec_info_nb_frames)
240  FAIL("no frame in rv30/40 and no sar");
241  break;
242  case AVMEDIA_TYPE_SUBTITLE:
243  if (st->codecpar->codec_id == AV_CODEC_ID_HDMV_PGS_SUBTITLE && !st->codecpar->width)
244  FAIL("unspecified size");
245  break;
246  case AVMEDIA_TYPE_DATA:
247  if(st->codecpar->codec_id == AV_CODEC_ID_NONE) return 1;
248  break;
249  default:
250  break;
251  }
252 
253  if (st->codecpar->codec_id == AV_CODEC_ID_NONE)
254  FAIL("unknown codec");
255  return 1;
256 }
257 
258 static bool force_sw_decode(AVCodecContext *avctx)
259 {
260  switch (avctx->codec_id)
261  {
262  case AV_CODEC_ID_H264:
263  switch (avctx->profile)
264  {
265  case FF_PROFILE_H264_HIGH_10:
266  case FF_PROFILE_H264_HIGH_10_INTRA:
267  case FF_PROFILE_H264_HIGH_422:
268  case FF_PROFILE_H264_HIGH_422_INTRA:
269  case FF_PROFILE_H264_HIGH_444_PREDICTIVE:
270  case FF_PROFILE_H264_HIGH_444_INTRA:
271  case FF_PROFILE_H264_CAVLC_444:
272  return true;
273  default:
274  break;
275  }
276  break;
277  default:
278  break;
279  }
280  return false;
281 }
282 
283 static void myth_av_log(void *ptr, int level, const char* fmt, va_list vl)
284 {
286  return;
287 
288  if (VERBOSE_LEVEL_NONE)
289  return;
290 
291  static QString full_line("");
292  static const int msg_len = 255;
293  static QMutex string_lock;
294  uint64_t verbose_mask = VB_LIBAV;
295  LogLevel_t verbose_level = LOG_EMERG;
296 
297  // determine mythtv debug level from av log level
298  switch (level)
299  {
300  case AV_LOG_PANIC:
301  verbose_level = LOG_EMERG;
302  verbose_mask |= VB_GENERAL;
303  break;
304  case AV_LOG_FATAL:
305  verbose_level = LOG_CRIT;
306  verbose_mask |= VB_GENERAL;
307  break;
308  case AV_LOG_ERROR:
309  verbose_level = LOG_ERR;
310  break;
311  case AV_LOG_WARNING:
312  verbose_level = LOG_WARNING;
313  break;
314  case AV_LOG_INFO:
315  verbose_level = LOG_INFO;
316  break;
317  case AV_LOG_VERBOSE:
318  case AV_LOG_DEBUG:
319  case AV_LOG_TRACE:
320  verbose_level = LOG_DEBUG;
321  break;
322  default:
323  return;
324  }
325 
326  if (!VERBOSE_LEVEL_CHECK(verbose_mask, verbose_level))
327  return;
328 
329  string_lock.lock();
330  if (full_line.isEmpty() && ptr) {
331  AVClass* avc = *(AVClass**)ptr;
332  full_line.sprintf("[%s @ %p] ", avc->item_name(ptr), avc);
333  }
334 
335  char str[msg_len+1];
336  int bytes = vsnprintf(str, msg_len+1, fmt, vl);
337 
338  // check for truncated messages and fix them
339  if (bytes > msg_len)
340  {
341  LOG(VB_GENERAL, LOG_WARNING,
342  QString("Libav log output truncated %1 of %2 bytes written")
343  .arg(msg_len).arg(bytes));
344  str[msg_len-1] = '\n';
345  }
346 
347  full_line += QString(str);
348  if (full_line.endsWith("\n"))
349  {
350  LOG(verbose_mask, verbose_level, full_line.trimmed());
351  full_line.truncate(0);
352  }
353  string_lock.unlock();
354 }
355 
356 static int get_canonical_lang(const char *lang_cstr)
357 {
358  if (lang_cstr[0] == '\0' || lang_cstr[1] == '\0')
359  {
360  return iso639_str3_to_key("und");
361  }
362  else if (lang_cstr[2] == '\0')
363  {
364  QString tmp2 = lang_cstr;
365  QString tmp3 = iso639_str2_to_str3(tmp2);
366  int lang = iso639_str3_to_key(tmp3);
367  return iso639_key_to_canonical_key(lang);
368  }
369  else
370  {
371  int lang = iso639_str3_to_key(lang_cstr);
372  return iso639_key_to_canonical_key(lang);
373  }
374 }
375 
377 {
378  opts.decoders->append("ffmpeg");
379  (*opts.equiv_decoders)["ffmpeg"].append("nuppel");
380  (*opts.equiv_decoders)["ffmpeg"].append("dummy");
381 
382 #ifdef USING_VDPAU
383  opts.decoders->append("vdpau");
384  (*opts.equiv_decoders)["vdpau"].append("dummy");
385 #endif
386 #ifdef USING_DXVA2
387  opts.decoders->append("dxva2");
388  (*opts.equiv_decoders)["dxva2"].append("dummy");
389 #endif
390 
391 #ifdef USING_VAAPI
392  opts.decoders->append("vaapi");
393  (*opts.equiv_decoders)["vaapi"].append("dummy");
394 #endif
395 #ifdef USING_VAAPI2
396  opts.decoders->append("vaapi2");
397  (*opts.equiv_decoders)["vaapi2"].append("dummy");
398 #endif
399 #ifdef USING_MEDIACODEC
400  opts.decoders->append("mediacodec");
401  (*opts.equiv_decoders)["mediacodec"].append("dummy");
402 #endif
403 
405 }
406 
408  const ProgramInfo &pginfo,
409  PlayerFlags flags)
410  : DecoderBase(parent, pginfo),
411  private_dec(nullptr),
412  is_db_ignored(gCoreContext->IsDatabaseIgnored()),
413  m_h264_parser(new H264Parser()),
414  ic(nullptr),
415  frame_decoded(0), decoded_video_frame(nullptr),
416  avfRingBuffer(nullptr), sws_ctx(nullptr),
417  directrendering(false),
418  no_dts_hack(false), dorewind(false),
419  gopset(false), seen_gop(false),
420  seq_count(0),
421  prevgoppos(0), gotVideoFrame(false),
422  hasVideo(false), needDummyVideoFrames(false),
423  skipaudio(false), allowedquit(false),
424  start_code_state(0xffffffff),
425  lastvpts(0), lastapts(0),
426  lastccptsu(0),
427  firstvpts(0), firstvptsinuse(false),
428  faulty_pts(0), faulty_dts(0),
429  last_pts_for_fault_detection(0),
430  last_dts_for_fault_detection(0),
431  pts_detected(false),
432  reordered_pts_detected(false),
433  pts_selected(true),
434  use_frame_timing(false),
435  force_dts_timestamps(false),
436  playerFlags(flags),
437  video_codec_id(kCodec_NONE),
438  maxkeyframedist(-1),
439  averror_count(0),
440  // Closed Caption & Teletext decoders
441  ignore_scte(0),
442  invert_scte_field(0),
443  last_scte_field(0),
444  ccd608(new CC608Decoder(parent->GetCC608Reader())),
445  ccd708(new CC708Decoder(parent->GetCC708Reader())),
446  ttd(new TeletextDecoder(parent->GetTeletextReader())),
447  // Interactive TV
448  itv(nullptr),
449  // Audio
450  disable_passthru(false),
451  m_fps(0.0f),
452  codec_is_mpeg(false),
453  m_processFrames(true),
454  m_streams_changed(false)
455 {
456  memset(&readcontext, 0, sizeof(readcontext));
457  memset(ccX08_in_pmt, 0, sizeof(ccX08_in_pmt));
458  memset(ccX08_in_tracks, 0, sizeof(ccX08_in_tracks));
459 
460  audioSamples = (uint8_t *)av_mallocz(AudioOutput::MAX_SIZE_BUFFER);
461  ccd608->SetIgnoreTimecode(true);
462 
463  av_log_set_callback(myth_av_log);
464 
465  audioIn.sample_size = -32; // force SetupAudioStream to run once
467 
469 
470  SetIdrOnlyKeyframes(true);
471  m_audioReadAhead = gCoreContext->GetNumSetting("AudioReadAhead", 100);
472 
473  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("PlayerFlags: 0x%1, AudioReadAhead: %2 msec")
474  .arg(playerFlags, 0, 16).arg(m_audioReadAhead));
475 }
476 
478 {
479  while (!storedPackets.isEmpty())
480  {
481  AVPacket *pkt = storedPackets.takeFirst();
482  av_packet_unref(pkt);
483  delete pkt;
484  }
485 
486  CloseContext();
487  delete ccd608;
488  delete ccd708;
489  delete ttd;
490  delete private_dec;
491  delete m_h264_parser;
492  delete m_mythcodecctx;
493 
494  sws_freeContext(sws_ctx);
495 
496  av_freep(&audioSamples);
497 
498  if (avfRingBuffer)
499  delete avfRingBuffer;
500 
501  if (LCD *lcd = LCD::Get())
502  {
503  lcd->setAudioFormatLEDs(AUDIO_AC3, false);
504  lcd->setVideoFormatLEDs(VIDEO_MPG, false);
505  lcd->setVariousLEDs(VARIOUS_HDTV, false);
506  lcd->setVariousLEDs(VARIOUS_SPDIF, false);
507  lcd->setSpeakerLEDs(SPEAKER_71, false); // should clear any and all speaker LEDs
508  }
509 }
510 
512 {
513  if (ic)
514  {
515  for (uint i = 0; i < ic->nb_streams; i++)
516  {
517  QMutexLocker locker(avcodeclock);
518  AVStream *st = ic->streams[i];
520  }
521  }
522 }
523 
525 {
526  if (ic)
527  {
528  CloseCodecs();
529 
530  AVInputFormat *fmt = ic->iformat;
531  ic->iformat->flags |= AVFMT_NOFILE;
532 
533  av_free(ic->pb->buffer);
534  av_free(ic->pb);
535  avformat_close_input(&ic);
536  ic = nullptr;
537  fmt->flags &= ~AVFMT_NOFILE;
538  }
539 
540  delete private_dec;
541  private_dec = nullptr;
542  m_h264_parser->Reset();
543 }
544 
545 static int64_t lsb3full(int64_t lsb, int64_t base_ts, int lsb_bits)
546 {
547  int64_t mask = (lsb_bits < 64) ? (1LL<<lsb_bits)-1 : -1LL;
548  return ((lsb - base_ts)&mask);
549 }
550 
552 {
553  int64_t start_pts = 0, pts;
554 
555  AVStream *st = nullptr;
556  for (uint i = 0; i < ic->nb_streams; i++)
557  {
558  AVStream *st1 = ic->streams[i];
559  if (st1 && st1->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
560  {
561  st = st1;
562  break;
563  }
564  }
565  if (!st)
566  return false;
567 
568  if (ic->start_time != AV_NOPTS_VALUE)
569  start_pts = av_rescale(ic->start_time,
570  st->time_base.den,
571  AV_TIME_BASE * (int64_t)st->time_base.num);
572 
573  pts = av_rescale(timecode / 1000.0,
574  st->time_base.den,
575  st->time_base.num);
576 
577  // adjust for start time and wrap
578  pts = lsb3full(pts, start_pts, st->pts_wrap_bits);
579 
580  return (int64_t)(av_q2d(st->time_base) * pts * 1000);
581 }
582 
584  int64_t timecode)
585 {
586  int64_t start_pts = 0, pts;
587 
588  if (ic->start_time != AV_NOPTS_VALUE)
589  start_pts = av_rescale(ic->start_time,
590  st->time_base.den,
591  AV_TIME_BASE * (int64_t)st->time_base.num);
592 
593  pts = av_rescale(timecode / 1000.0,
594  st->time_base.den,
595  st->time_base.num);
596 
597  // adjust for start time and wrap
598  pts = lsb3full(pts, start_pts, st->pts_wrap_bits);
599 
600  return (int64_t)(av_q2d(st->time_base) * pts * 1000);
601 }
602 
604 {
605  if (ic && ic->nb_chapters > 1)
606  return ic->nb_chapters;
607  return 0;
608 }
609 
610 void AvFormatDecoder::GetChapterTimes(QList<long long> &times)
611 {
612  int total = GetNumChapters();
613  if (!total)
614  return;
615 
616  for (int i = 0; i < total; i++)
617  {
618  int num = ic->chapters[i]->time_base.num;
619  int den = ic->chapters[i]->time_base.den;
620  int64_t start = ic->chapters[i]->start;
621  long double total_secs = (long double)start * (long double)num /
622  (long double)den;
623  times.push_back((long long)total_secs);
624  }
625 }
626 
627 int AvFormatDecoder::GetCurrentChapter(long long framesPlayed)
628 {
629  if (!GetNumChapters())
630  return 0;
631 
632  for (int i = (ic->nb_chapters - 1); i > -1 ; i--)
633  {
634  int num = ic->chapters[i]->time_base.num;
635  int den = ic->chapters[i]->time_base.den;
636  int64_t start = ic->chapters[i]->start;
637  long double total_secs = (long double)start * (long double)num /
638  (long double)den;
639  long long framenum = (long long)(total_secs * fps);
640  if (framesPlayed >= framenum)
641  {
642  LOG(VB_PLAYBACK, LOG_INFO, LOC +
643  QString("GetCurrentChapter(selected chapter %1 framenum %2)")
644  .arg(i + 1).arg(framenum));
645  return i + 1;
646  }
647  }
648  return 0;
649 }
650 
651 long long AvFormatDecoder::GetChapter(int chapter)
652 {
653  if (chapter < 1 || chapter > GetNumChapters())
654  return -1;
655 
656  int num = ic->chapters[chapter - 1]->time_base.num;
657  int den = ic->chapters[chapter - 1]->time_base.den;
658  int64_t start = ic->chapters[chapter - 1]->start;
659  long double total_secs = (long double)start * (long double)num /
660  (long double)den;
661  long long framenum = (long long)(total_secs * fps);
662  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("GetChapter %1: framenum %2")
663  .arg(chapter).arg(framenum));
664  return framenum;
665 }
666 
667 bool AvFormatDecoder::DoRewind(long long desiredFrame, bool discardFrames)
668 {
669  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("DoRewind(%1, %2 discard frames)")
670  .arg(desiredFrame).arg( discardFrames ? "do" : "don't" ));
671 
673  return DecoderBase::DoRewind(desiredFrame, discardFrames);
674 
675  dorewind = true;
676 
677  // avformat-based seeking
678  return DoFastForward(desiredFrame, discardFrames);
679 }
680 
681 bool AvFormatDecoder::DoFastForward(long long desiredFrame, bool discardFrames)
682 {
683  LOG(VB_PLAYBACK, LOG_INFO, LOC +
684  QString("DoFastForward(%1 (%2), %3 discard frames)")
685  .arg(desiredFrame).arg(framesPlayed)
686  .arg((discardFrames) ? "do" : "don't"));
687 
689  return DecoderBase::DoFastForward(desiredFrame, discardFrames);
690 
691  bool oldrawstate = getrawframes;
692  getrawframes = false;
693 
694  AVStream *st = nullptr;
695  for (uint i = 0; i < ic->nb_streams; i++)
696  {
697  AVStream *st1 = ic->streams[i];
698  if (st1 && st1->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
699  {
700  st = st1;
701  break;
702  }
703  }
704 
705  int seekDelta = desiredFrame - framesPlayed;
706 
707  // avoid using av_frame_seek if we are seeking frame-by-frame when paused
708  if (seekDelta >= 0 && seekDelta < 2 && !dorewind && m_parent->GetPlaySpeed() == 0.0f)
709  {
710  SeekReset(framesPlayed, seekDelta, false, true);
712  getrawframes = oldrawstate;
713  return true;
714  }
715 
716  long long ts = 0;
717  if (ic->start_time != AV_NOPTS_VALUE)
718  ts = ic->start_time;
719 
720  // convert framenumber to normalized timestamp
721  long double seekts = desiredFrame * AV_TIME_BASE / fps;
722  ts += (long long)seekts;
723 
724  // XXX figure out how to do snapping in this case
725  bool exactseeks = !DecoderBase::GetSeekSnap();
726 
727  int flags = (dorewind || exactseeks) ? AVSEEK_FLAG_BACKWARD : 0;
728 
729  if (av_seek_frame(ic, -1, ts, flags) < 0)
730  {
731  LOG(VB_GENERAL, LOG_ERR, LOC +
732  QString("av_seek_frame(ic, -1, %1, 0) -- error").arg(ts));
733  getrawframes = oldrawstate;
734  return false;
735  }
736 
737  int normalframes = 0;
738 
739  if (st && st->cur_dts != AV_NOPTS_VALUE)
740  {
741 
742  int64_t adj_cur_dts = st->cur_dts;
743 
744  if (ic->start_time != AV_NOPTS_VALUE)
745  {
746  int64_t st1 = av_rescale(ic->start_time,
747  st->time_base.den,
748  AV_TIME_BASE * (int64_t)st->time_base.num);
749  adj_cur_dts = lsb3full(adj_cur_dts, st1, st->pts_wrap_bits);
750  }
751 
752  int64_t adj_seek_dts = av_rescale(seekts,
753  st->time_base.den,
754  AV_TIME_BASE * (int64_t)st->time_base.num);
755 
756  int64_t max_dts = (st->pts_wrap_bits < 64) ? (1LL<<st->pts_wrap_bits)-1 : -1LL;
757 
758  // When seeking near the start of a stream the current dts is sometimes
759  // less than the start time which causes lsb3full to return adj_cur_dts
760  // close to the maximum dts value. If so, set adj_cur_dts to zero.
761  if (adj_seek_dts < max_dts / 64 && adj_cur_dts > max_dts / 2)
762  adj_cur_dts = 0;
763 
764  long long newts = av_rescale(adj_cur_dts,
765  (int64_t)AV_TIME_BASE *
766  (int64_t)st->time_base.num,
767  st->time_base.den);
768 
769  lastKey = (long long)((newts*(long double)fps)/AV_TIME_BASE);
771  fpsSkip = 0;
773 
774  normalframes = (exactseeks) ? desiredFrame - framesPlayed : 0;
775  normalframes = max(normalframes, 0);
776  no_dts_hack = false;
777  }
778  else
779  {
780  LOG(VB_GENERAL, LOG_INFO, LOC + "No DTS Seeking Hack!");
781  no_dts_hack = true;
782  framesPlayed = desiredFrame;
783  fpsSkip = 0;
784  framesRead = desiredFrame;
785  normalframes = 0;
786  }
787 
788  SeekReset(lastKey, normalframes, true, discardFrames);
789 
790  if (discardFrames)
792 
793  dorewind = false;
794 
795  getrawframes = oldrawstate;
796 
797  return true;
798 }
799 
800 void AvFormatDecoder::SeekReset(long long newKey, uint skipFrames,
801  bool doflush, bool discardFrames)
802 {
803  if (!ringBuffer)
804  return; // nothing to reset...
805 
806  LOG(VB_PLAYBACK, LOG_INFO, LOC +
807  QString("SeekReset(%1, %2, %3 flush, %4 discard)")
808  .arg(newKey).arg(skipFrames)
809  .arg((doflush) ? "do" : "don't")
810  .arg((discardFrames) ? "do" : "don't"));
811 
812  DecoderBase::SeekReset(newKey, skipFrames, doflush, discardFrames);
813 
814  QMutexLocker locker(avcodeclock);
815 
816  if (doflush)
817  {
818  lastapts = 0;
819  lastvpts = 0;
820  lastccptsu = 0;
821  faulty_pts = faulty_dts = 0;
824  pts_detected = false;
825  reordered_pts_detected = false;
826 
827  ff_read_frame_flush(ic);
828 
829  // Only reset the internal state if we're using our seeking,
830  // not when using libavformat's seeking
832  {
833  ic->pb->pos = ringBuffer->GetReadPosition();
834  ic->pb->buf_ptr = ic->pb->buffer;
835  ic->pb->buf_end = ic->pb->buffer;
836  ic->pb->eof_reached = 0;
837  }
838 
839  // Flush the avcodec buffers
840  LOG(VB_PLAYBACK, LOG_INFO, LOC + "SeekReset() flushing");
841  for (uint i = 0; i < ic->nb_streams; i++)
842  {
843  AVCodecContext *enc = gCodecMap->hasCodecContext(ic->streams[i]);
844  // note that contexts that have not been opened have
845  // enc->internal = nullptr and cause a segfault in
846  // avcodec_flush_buffers
847  if (enc && enc->internal)
848  avcodec_flush_buffers(enc);
849  }
850  if (private_dec)
851  private_dec->Reset();
852  }
853 
854  // Discard all the queued up decoded frames
855  if (discardFrames)
856  m_parent->DiscardVideoFrames(doflush);
857 
858  if (doflush)
859  {
860  // Free up the stored up packets
861  while (!storedPackets.isEmpty())
862  {
863  AVPacket *pkt = storedPackets.takeFirst();
864  av_packet_unref(pkt);
865  delete pkt;
866  }
867 
868  prevgoppos = 0;
869  gopset = false;
870  if (!ringBuffer->IsDVD())
871  {
872  if (!no_dts_hack)
873  {
875  fpsSkip = 0;
877  }
878 
879  no_dts_hack = false;
880  }
881  }
882 
883  // Skip all the desired number of skipFrames
884 
885  // Some seeks can be very slow. The most common example comes
886  // from HD-PVR recordings, where keyframes are 128 frames apart
887  // and decoding (even hardware decoding) may not be much faster
888  // than realtime, causing some exact seeks to take 2-4 seconds.
889  // If exact seeking is not required, we take some shortcuts.
890  // First, we impose an absolute maximum time we are willing to
891  // spend (maxSeekTimeMs) on the forward frame-by-frame skip.
892  // After that much time has elapsed, we give up and stop the
893  // frame-by-frame seeking. Second, after skipping a few frames,
894  // we predict whether the situation is hopeless, i.e. the total
895  // skipping would take longer than giveUpPredictionMs, and if so,
896  // stop skipping right away.
897  bool exactSeeks = !GetSeekSnap();
898  const int maxSeekTimeMs = 200;
899  int profileFrames = 0;
901  for (; (skipFrames > 0 && !ateof &&
902  (exactSeeks || begin.elapsed() < maxSeekTimeMs));
903  --skipFrames, ++profileFrames)
904  {
907  {
909  decoded_video_frame = nullptr;
910  }
911  if (!exactSeeks && profileFrames >= 5 && profileFrames < 10)
912  {
913  const int giveUpPredictionMs = 400;
914  int remainingTimeMs =
915  skipFrames * (float)begin.elapsed() / profileFrames;
916  if (remainingTimeMs > giveUpPredictionMs)
917  {
918  LOG(VB_PLAYBACK, LOG_DEBUG,
919  QString("Frame-by-frame seeking would take "
920  "%1 ms to finish, skipping.").arg(remainingTimeMs));
921  break;
922  }
923  }
924  }
925 
926  if (doflush)
927  {
928  firstvpts = 0;
929  firstvptsinuse = true;
930  }
931 }
932 
934 {
935  if (!eof && ic && ic->pb)
936  {
937  LOG(VB_GENERAL, LOG_NOTICE, LOC +
938  QString("Resetting byte context eof (livetv %1 was eof %2)")
939  .arg(livetv).arg(ic->pb->eof_reached));
940  ic->pb->eof_reached = 0;
941  }
942  DecoderBase::SetEof(eof);
943 }
944 
945 void AvFormatDecoder::Reset(bool reset_video_data, bool seek_reset,
946  bool reset_file)
947 {
948  LOG(VB_PLAYBACK, LOG_INFO, LOC +
949  QString("Reset: Video %1, Seek %2, File %3")
950  .arg(reset_video_data).arg(seek_reset).arg(reset_file));
951 
952  if (seek_reset)
953  SeekReset(0, 0, true, false);
954 
955  DecoderBase::Reset(reset_video_data, false, reset_file);
956 
957  if (reset_video_data)
958  {
959  seen_gop = false;
960  seq_count = 0;
961  }
962 }
963 
965  const QString &filename, int testbufsize)
966 {
967  AVProbeData probe;
968  memset(&probe, 0, sizeof(AVProbeData));
969 
970  QByteArray fname = filename.toLatin1();
971  probe.filename = fname.constData();
972  probe.buf = (unsigned char *)testbuf;
973  probe.buf_size = testbufsize;
974 
975  int score = AVPROBE_SCORE_MAX/4;
976 
977  if (testbufsize + AVPROBE_PADDING_SIZE > kDecoderProbeBufferSize)
978  {
979  probe.buf_size = kDecoderProbeBufferSize - AVPROBE_PADDING_SIZE;
980  score = 0;
981  }
982  else if (testbufsize*2 >= kDecoderProbeBufferSize)
983  {
984  score--;
985  }
986 
987  if (av_probe_input_format2(&probe, true, &score))
988  return true;
989  return false;
990 }
991 
993 {
994  int buf_size = ringBuffer->BestBufferSize();
995  int streamed = ringBuffer->IsStreamed();
997  readcontext.flags = AVIO_FLAG_READ;
998  readcontext.is_streamed = streamed;
999  readcontext.max_packet_size = 0;
1000  readcontext.priv_data = avfRingBuffer;
1001  unsigned char* buffer = (unsigned char *)av_malloc(buf_size);
1002  ic->pb = avio_alloc_context(buffer, buf_size, 0,
1003  &readcontext,
1007 
1008  // We can always seek during LiveTV
1009  ic->pb->seekable = !streamed || forceseek;
1010  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("Buffer size: %1 streamed %2 seekable %3")
1011  .arg(buf_size).arg(streamed).arg(ic->pb->seekable));
1012 }
1013 
1014 extern "C" void HandleStreamChange(void *data)
1015 {
1016  AvFormatDecoder *decoder =
1017  reinterpret_cast<AvFormatDecoder*>(data);
1018 
1019  int cnt = decoder->ic->nb_streams;
1020 
1021  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1022  QString("streams_changed 0x%1 -- stream count %2")
1023  .arg((uint64_t)data,0,16).arg(cnt));
1024 
1025  decoder->m_streams_changed = true;
1026 }
1027 
1029 {
1030  QMutexLocker lock(avcodeclock);
1031  int retval = avformat_find_stream_info(ic, nullptr);
1032  silence_ffmpeg_logging = false;
1033  // ffmpeg 3.0 is returning -1 code when there is a channel
1034  // change or some encoding error just after the start
1035  // of the file, but is has found the correct stream info
1036  // Set rc to 0 so that playing can continue.
1037  if (retval == -1)
1038  retval = 0;
1039  return retval;
1040 }
1041 
1056 int AvFormatDecoder::OpenFile(RingBuffer *rbuffer, bool novideo,
1057  char testbuf[kDecoderProbeBufferSize],
1058  int testbufsize)
1059 {
1060  CloseContext();
1061 
1062  ringBuffer = rbuffer;
1063 
1064  // Process frames immediately unless we're decoding
1065  // a DVD, in which case don't so that we don't show
1066  // anything whilst probing the data streams.
1068 
1069  if (avfRingBuffer)
1070  delete avfRingBuffer;
1071  avfRingBuffer = new AVFRingBuffer(rbuffer);
1072 
1073  AVInputFormat *fmt = nullptr;
1074  QString fnames = ringBuffer->GetFilename();
1075  QByteArray fnamea = fnames.toLatin1();
1076  const char *filename = fnamea.constData();
1077 
1078  AVProbeData probe;
1079  memset(&probe, 0, sizeof(AVProbeData));
1080  probe.filename = filename;
1081  probe.buf = (unsigned char *)testbuf;
1082  if (testbufsize + AVPROBE_PADDING_SIZE <= kDecoderProbeBufferSize)
1083  probe.buf_size = testbufsize;
1084  else
1085  probe.buf_size = kDecoderProbeBufferSize - AVPROBE_PADDING_SIZE;
1086 
1087  LOG(VB_PLAYBACK, LOG_DEBUG, LOC + "OpenFile -- begin");
1088 
1089  fmt = av_probe_input_format(&probe, true);
1090  if (!fmt)
1091  {
1092  LOG(VB_GENERAL, LOG_ERR, LOC +
1093  QString("Probe failed for file: \"%1\".").arg(filename));
1094  return -1;
1095  }
1096 
1097  if (!strcmp(fmt->name, "mpegts") &&
1098  gCoreContext->GetBoolSetting("FFMPEGTS", false))
1099  {
1100  AVInputFormat *fmt2 = av_find_input_format("mpegts-ffmpeg");
1101  if (fmt2)
1102  {
1103  fmt = fmt2;
1104  LOG(VB_GENERAL, LOG_INFO, LOC + "Using FFmpeg MPEG-TS demuxer (forced)");
1105  }
1106  }
1107 
1108  int err = 0;
1109  bool found = false;
1110  bool scanned = false;
1111 
1112  if (livetv)
1113  {
1114  // We try to open the file for up to 1.5 second using only buffer in memory
1115  MythTimer timer; timer.start();
1116 
1117  avfRingBuffer->SetInInit(true);
1118 
1119  while (!found && timer.elapsed() < 1500)
1120  {
1121  ic = avformat_alloc_context();
1122  if (!ic)
1123  {
1124  LOG(VB_GENERAL, LOG_ERR, LOC + "Could not allocate format context.");
1125  return -1;
1126  }
1127 
1128  InitByteContext(true);
1129 
1130  err = avformat_open_input(&ic, filename, fmt, nullptr);
1131  if (err < 0)
1132  {
1133  LOG(VB_PLAYBACK, LOG_DEBUG, LOC +
1134  QString("avformat_open_input failed for in ram data after %1ms, retrying in 50ms")
1135  .arg(timer.elapsed()));
1136  usleep(50 * 1000); // wait 50ms
1137  continue;
1138  }
1139 
1140  // Test if we can find all streams details in what has been found so far
1141  if (FindStreamInfo() < 0)
1142  {
1143  avformat_close_input(&ic);
1144  LOG(VB_PLAYBACK, LOG_DEBUG, LOC +
1145  QString("FindStreamInfo failed for in ram data after %1ms, retrying in 50ms")
1146  .arg(timer.elapsed()));
1147  usleep(50 * 1000); // wait 50ms
1148  continue;
1149  }
1150 
1151  found = true;
1152 
1153  for (uint i = 0; i < ic->nb_streams; i++)
1154  {
1155  if (!has_codec_parameters(ic->streams[i]))
1156  {
1157  avformat_close_input(&ic);
1158  found = false;
1159  LOG(VB_PLAYBACK, LOG_DEBUG, LOC +
1160  QString("Invalid streams found in ram data after %1ms, retrying in 50ms")
1161  .arg(timer.elapsed()));
1162  usleep(50 * 1000); // wait 50ms
1163  break;
1164  }
1165  }
1166  }
1167 
1168  avfRingBuffer->SetInInit(false);
1169 
1170  if (found)
1171  {
1172  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1173  QString("File successfully opened after %1ms")
1174  .arg(timer.elapsed()));
1175  }
1176  else
1177  {
1178  LOG(VB_PLAYBACK, LOG_WARNING, LOC +
1179  QString("No streams found in ram data after %1ms, defaulting to in-file")
1180  .arg(timer.elapsed()));
1181  }
1182  scanned = found;
1183  }
1184 
1185  // If we haven't opened the file so far, revert to old method
1186  while (!found)
1187  {
1188  ic = avformat_alloc_context();
1189  if (!ic)
1190  {
1191  LOG(VB_GENERAL, LOG_ERR, LOC + "Could not allocate format context.");
1192  return -1;
1193  }
1194 
1195  InitByteContext();
1196 
1197  err = avformat_open_input(&ic, filename, fmt, nullptr);
1198  if (err < 0)
1199  {
1200  if (!strcmp(fmt->name, "mpegts"))
1201  {
1202  fmt = av_find_input_format("mpegts-ffmpeg");
1203  if (fmt)
1204  {
1205  LOG(VB_GENERAL, LOG_ERR, LOC +
1206  QString("avformat_open_input failed with '%1' error.").arg(err));
1207  LOG(VB_GENERAL, LOG_ERR, LOC +
1208  QString("Attempting using original FFmpeg MPEG-TS demuxer."));
1209  // ic would have been freed due to the earlier failure
1210  continue;
1211  }
1212  break;
1213  }
1214  }
1215  found = true;
1216  }
1217  if (err < 0)
1218  {
1219  LOG(VB_GENERAL, LOG_ERR, LOC +
1220  QString("avformat err(%1) on avformat_open_input call.").arg(err));
1221  ic = nullptr;
1222  return -1;
1223  }
1224 
1225  if (!scanned)
1226  {
1227  int ret = FindStreamInfo();
1228  if (ret < 0)
1229  {
1230  LOG(VB_GENERAL, LOG_ERR, LOC + "Could not find codec parameters. " +
1231  QString("file was \"%1\".").arg(filename));
1232  avformat_close_input(&ic);
1233  ic = nullptr;
1234  return -1;
1235  }
1236  }
1237 
1238  ic->streams_changed = HandleStreamChange;
1239  ic->stream_change_data = this;
1240 
1241  fmt->flags &= ~AVFMT_NOFILE;
1242 
1243  if (!livetv && !ringBuffer->IsDisc())
1244  {
1245  // generate timings based on the video stream to avoid bogus ffmpeg
1246  // values for duration and bitrate
1248  }
1249 
1250  // FLAC, MP3 or M4A file may contains an artwork image, a single frame MJPEG,
1251  // we need to ignore it as we don't handle single frames or images in place of video
1252  // TODO: display single frame
1253  QString extension = QFileInfo(fnames).suffix();
1254  if (!strcmp(fmt->name, "mp3") || !strcmp(fmt->name, "flac") ||
1255  !strcmp(fmt->name, "ogg") ||
1256  !extension.compare("m4a", Qt::CaseInsensitive))
1257  {
1258  novideo = true;
1259  }
1260 
1261  // Scan for the initial A/V streams
1262  int ret = ScanStreams(novideo);
1263  if (-1 == ret)
1264  return ret;
1265 
1266  AutoSelectTracks(); // This is needed for transcoder
1267 
1268 #ifdef USING_MHEG
1269  {
1270  int initialAudio = -1, initialVideo = -1;
1271  if (itv || (itv = m_parent->GetInteractiveTV()))
1272  itv->GetInitialStreams(initialAudio, initialVideo);
1273  if (initialAudio >= 0)
1274  SetAudioByComponentTag(initialAudio);
1275  if (initialVideo >= 0)
1276  SetVideoByComponentTag(initialVideo);
1277  }
1278 #endif // USING_MHEG
1279 
1280  // Try to get a position map from the recorder if we don't have one yet.
1282  {
1284  {
1287  {
1288  hasFullPositionMap = true;
1289  gopset = true;
1290  }
1291  }
1292  }
1293 
1294  // If watching pre-recorded television or video use the marked duration
1295  // from the db if it exists, else ffmpeg duration
1296  int64_t dur = 0;
1297 
1298  if (m_playbackinfo)
1299  {
1301  dur /= 1000;
1302  }
1303 
1304  if (dur == 0)
1305  {
1306  if ((ic->duration == AV_NOPTS_VALUE) &&
1307  (!livetv && !ringBuffer->IsDisc()))
1308  av_estimate_timings(ic, 0);
1309 
1310  dur = ic->duration / (int64_t)AV_TIME_BASE;
1311  }
1312 
1313  if (dur > 0 && !livetv && !watchingrecording)
1314  {
1315  m_parent->SetDuration((int)dur);
1316  }
1317 
1318  // If we don't have a position map, set up ffmpeg for seeking
1320  {
1321  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1322  "Recording has no position -- using libavformat seeking.");
1323 
1324  if (dur > 0)
1325  {
1326  m_parent->SetFileLength((int)(dur), (int)(dur * fps));
1327  }
1328  else
1329  {
1330  // the pvr-250 seems to over report the bitrate by * 2
1331  float bytespersec = (float)bitrate / 8 / 2;
1332  float secs = ringBuffer->GetRealFileSize() * 1.0f / bytespersec;
1333  m_parent->SetFileLength((int)(secs),
1334  (int)(secs * static_cast<float>(fps)));
1335  }
1336 
1337  // we will not see a position map from db or remote encoder,
1338  // set the gop interval to 15 frames. if we guess wrong, the
1339  // auto detection will change it.
1340  keyframedist = 15;
1342 
1343  if (!strcmp(fmt->name, "avi"))
1344  {
1345  // avi keyframes are too irregular
1346  keyframedist = 1;
1347  }
1348 
1349  dontSyncPositionMap = true;
1350  ic->build_index = 1;
1351  }
1352  // we have a position map, disable libavformat's seek index
1353  else
1354  ic->build_index = 0;
1355 
1356  av_dump_format(ic, 0, filename, 0);
1357 
1358  // print some useful information if playback debugging is on
1359  if (hasFullPositionMap)
1360  LOG(VB_PLAYBACK, LOG_INFO, LOC + "Position map found");
1361  else if (recordingHasPositionMap)
1362  LOG(VB_PLAYBACK, LOG_INFO, LOC + "Partial position map found");
1363  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1364  QString("Successfully opened decoder for file: \"%1\". novideo(%2)")
1365  .arg(filename).arg(novideo));
1366 
1367  // Print AVChapter information
1368  for (unsigned int i=0; i < ic->nb_chapters; i++)
1369  {
1370  int num = ic->chapters[i]->time_base.num;
1371  int den = ic->chapters[i]->time_base.den;
1372  int64_t start = ic->chapters[i]->start;
1373  long double total_secs = (long double)start * (long double)num /
1374  (long double)den;
1375  int hours = (int)total_secs / 60 / 60;
1376  int minutes = ((int)total_secs / 60) - (hours * 60);
1377  double secs = (double)total_secs -
1378  (double)(hours * 60 * 60 + minutes * 60);
1379  long long framenum = (long long)(total_secs * fps);
1380  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1381  QString("Chapter %1 found @ [%2:%3:%4]->%5")
1382  .arg(QString().sprintf("%02d", i + 1))
1383  .arg(QString().sprintf("%02d", hours))
1384  .arg(QString().sprintf("%02d", minutes))
1385  .arg(QString().sprintf("%06.3f", secs))
1386  .arg(framenum));
1387  }
1388 
1389  if (getenv("FORCE_DTS_TIMESTAMPS"))
1390  force_dts_timestamps = true;
1391 
1392  if (ringBuffer->IsDVD())
1393  {
1394  // Reset DVD playback and clear any of
1395  // our buffers so that none of the data
1396  // parsed so far to determine decoders
1397  // gets shown.
1399  return -1;
1400  ringBuffer->IgnoreWaitStates(false);
1401 
1402  Reset(true, true, true);
1403 
1404  // Now we're ready to process and show frames
1405  m_processFrames = true;
1406  }
1407 
1408 
1409  // Return true if recording has position map
1410  return recordingHasPositionMap;
1411 }
1412 
1413 float AvFormatDecoder::normalized_fps(AVStream *stream, AVCodecContext *enc)
1414 {
1415  float fps, avg_fps, codec_fps, container_fps, estimated_fps;
1416  avg_fps = codec_fps = container_fps = estimated_fps = 0.0f;
1417 
1418  if (stream->avg_frame_rate.den && stream->avg_frame_rate.num)
1419  avg_fps = av_q2d(stream->avg_frame_rate); // MKV default_duration
1420 
1421  if (enc->time_base.den && enc->time_base.num) // tbc
1422  codec_fps = 1.0 / av_q2d(enc->time_base) / enc->ticks_per_frame;
1423  // Some formats report fps waaay too high. (wrong time_base)
1424  if (codec_fps > 121.0f && (enc->time_base.den > 10000) &&
1425  (enc->time_base.num == 1))
1426  {
1427  enc->time_base.num = 1001; // seems pretty standard
1428  if (av_q2d(enc->time_base) > 0)
1429  codec_fps = 1.0 / av_q2d(enc->time_base);
1430  }
1431  if (stream->time_base.den && stream->time_base.num) // tbn
1432  container_fps = 1.0 / av_q2d(stream->time_base);
1433  if (stream->r_frame_rate.den && stream->r_frame_rate.num) // tbr
1434  estimated_fps = av_q2d(stream->r_frame_rate);
1435 
1436  // matroska demuxer sets the default_duration to avg_frame_rate
1437  // mov,mp4,m4a,3gp,3g2,mj2 demuxer sets avg_frame_rate
1438  if ((QString(ic->iformat->name).contains("matroska") ||
1439  QString(ic->iformat->name).contains("mov")) &&
1440  avg_fps < 121.0f && avg_fps > 3.0f)
1441  fps = avg_fps;
1442  else if (QString(ic->iformat->name).contains("avi") &&
1443  container_fps < 121.0f && container_fps > 3.0f)
1444  fps = container_fps; // avi uses container fps for timestamps
1445  else if (codec_fps < 121.0f && codec_fps > 3.0f)
1446  fps = codec_fps;
1447  else if (container_fps < 121.0f && container_fps > 3.0f)
1448  fps = container_fps;
1449  else if (estimated_fps < 121.0f && estimated_fps > 3.0f)
1450  fps = estimated_fps;
1451  else if (avg_fps < 121.0f && avg_fps > 3.0f)
1452  fps = avg_fps;
1453  else
1454  fps = 30000.0f / 1001.0f; // 29.97 fps
1455 
1456  if (fps != m_fps)
1457  {
1458  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1459  QString("Selected FPS is %1 (avg %2 codec %3 "
1460  "container %4 estimated %5)").arg(fps).arg(avg_fps)
1461  .arg(codec_fps).arg(container_fps).arg(estimated_fps));
1462  }
1463 
1464  return fps;
1465 }
1466 
1467 #ifdef USING_VDPAU
1468 static enum AVPixelFormat get_format_vdpau(struct AVCodecContext *avctx,
1469  const enum AVPixelFormat *valid_fmts)
1470 {
1471  AvFormatDecoder *nd = (AvFormatDecoder *)(avctx->opaque);
1472  MythPlayer *player = nullptr;
1473  VideoOutputVDPAU *videoOut = nullptr;
1474  if (nd)
1475  player = nd->GetPlayer();
1476  if (player)
1477  videoOut = (VideoOutputVDPAU*)(player->GetVideoOutput());
1478 
1479  if (videoOut)
1480  {
1481  static uint8_t *dummy[1] = { nullptr };
1482  avctx->hwaccel_context = player->GetDecoderContext(nullptr, dummy[0]);
1483  MythRenderVDPAU *render = videoOut->getRender();
1484  render->BindContext(avctx);
1485  if (avctx->hwaccel_context)
1486  {
1487  ((AVVDPAUContext*)(avctx->hwaccel_context))->render2 =
1489  }
1490  }
1491 
1492  while (*valid_fmts != AV_PIX_FMT_NONE) {
1493  if (avctx->hwaccel_context and (*valid_fmts == AV_PIX_FMT_VDPAU))
1494  return AV_PIX_FMT_VDPAU;
1495  if (not avctx->hwaccel_context and (*valid_fmts == AV_PIX_FMT_YUV420P))
1496  return AV_PIX_FMT_YUV420P;
1497  valid_fmts++;
1498  }
1499  return AV_PIX_FMT_NONE;
1500 }
1501 #endif
1502 
1503 #ifdef USING_DXVA2
1504 // Declared separately to allow attribute
1505 static enum AVPixelFormat get_format_dxva2(struct AVCodecContext *,
1506  const enum AVPixelFormat *);
1507 
1508 enum AVPixelFormat get_format_dxva2(struct AVCodecContext *avctx,
1509  const enum AVPixelFormat *valid_fmts)
1510 {
1511  AvFormatDecoder *nd = (AvFormatDecoder *)(avctx->opaque);
1512  if (nd && nd->GetPlayer())
1513  {
1514  static uint8_t *dummy[1] = { nullptr };
1515  avctx->hwaccel_context =
1516  (dxva_context*)nd->GetPlayer()->GetDecoderContext(nullptr, dummy[0]);
1517  }
1518 
1519  while (*valid_fmts != AV_PIX_FMT_NONE) {
1520  if (avctx->hwaccel_context and (*valid_fmts == AV_PIX_FMT_DXVA2_VLD))
1521  return AV_PIX_FMT_DXVA2_VLD;
1522  if (not avctx->hwaccel_context and (*valid_fmts == AV_PIX_FMT_YUV420P))
1523  return AV_PIX_FMT_YUV420P;
1524  valid_fmts++;
1525  }
1526  return AV_PIX_FMT_NONE;
1527 }
1528 #endif
1529 
1530 #if defined(USING_VAAPI) || defined(USING_VAAPI2)
1531 static bool IS_VAAPI_PIX_FMT(enum AVPixelFormat fmt)
1532 {
1533  return fmt == AV_PIX_FMT_VAAPI_MOCO ||
1534  fmt == AV_PIX_FMT_VAAPI_IDCT ||
1535  fmt == AV_PIX_FMT_VAAPI_VLD;
1536 }
1537 #endif
1538 
1539 #ifdef USING_VAAPI
1540 // Declared separately to allow attribute
1541 static enum AVPixelFormat get_format_vaapi(struct AVCodecContext *,
1542  const enum AVPixelFormat *);
1543 
1544 enum AVPixelFormat get_format_vaapi(struct AVCodecContext *avctx,
1545  const enum AVPixelFormat *valid_fmts)
1546 {
1547  AvFormatDecoder *nd = (AvFormatDecoder *)(avctx->opaque);
1548  if (nd && nd->GetPlayer())
1549  {
1550  static uint8_t *dummy[1] = { nullptr };
1551  avctx->hwaccel_context =
1552  (vaapi_context*)nd->GetPlayer()->GetDecoderContext(nullptr, dummy[0]);
1553  }
1554 
1555  while (*valid_fmts != AV_PIX_FMT_NONE) {
1556  if (avctx->hwaccel_context and (*valid_fmts == AV_PIX_FMT_VAAPI_VLD))
1557  return AV_PIX_FMT_VAAPI_VLD;
1558  if (not avctx->hwaccel_context and (*valid_fmts == AV_PIX_FMT_YUV420P))
1559  return AV_PIX_FMT_YUV420P;
1560  valid_fmts++;
1561  }
1562  return AV_PIX_FMT_NONE;
1563 }
1564 #endif
1565 
1566 #ifdef USING_VAAPI2
1567 static enum AVPixelFormat get_format_vaapi2(struct AVCodecContext */*avctx*/,
1568  const enum AVPixelFormat *valid_fmts)
1569 {
1570  enum AVPixelFormat ret = AV_PIX_FMT_NONE;
1571  while (*valid_fmts != AV_PIX_FMT_NONE) {
1572  if (IS_VAAPI_PIX_FMT(*valid_fmts))
1573  {
1574  ret = *valid_fmts;
1575  break;
1576  }
1577  valid_fmts++;
1578  }
1579  return ret;
1580 }
1581 #endif
1582 
1583 #ifdef USING_MEDIACODEC
1584 static enum AVPixelFormat get_format_mediacodec(struct AVCodecContext */*avctx*/,
1585  const enum AVPixelFormat *valid_fmts)
1586 {
1587  enum AVPixelFormat ret = AV_PIX_FMT_NONE;
1588  while (*valid_fmts != AV_PIX_FMT_NONE) {
1589  if (*valid_fmts == AV_PIX_FMT_MEDIACODEC)
1590  {
1591  ret = AV_PIX_FMT_MEDIACODEC;
1592  break;
1593  }
1594  valid_fmts++;
1595  }
1596  return ret;
1597 }
1598 #endif
1599 
1600 
1601 static bool IS_DR1_PIX_FMT(const enum AVPixelFormat fmt)
1602 {
1603  switch (fmt)
1604  {
1605  case AV_PIX_FMT_YUV420P:
1606  case AV_PIX_FMT_YUVJ420P:
1607  return true;
1608  default:
1609  return false;
1610  }
1611 }
1612 
1613 void AvFormatDecoder::InitVideoCodec(AVStream *stream, AVCodecContext *enc,
1614  bool selectedStream)
1615 {
1616  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1617  QString("InitVideoCodec() 0x%1 id(%2) type (%3).")
1618  .arg((uint64_t)enc,0,16)
1619  .arg(ff_codec_id_string(enc->codec_id))
1620  .arg(ff_codec_type_string(enc->codec_type)));
1621 
1622  if (ringBuffer && ringBuffer->IsDVD())
1623  directrendering = false;
1624 
1625  enc->opaque = (void *)this;
1626  enc->get_buffer2 = get_avf_buffer;
1627  enc->slice_flags = 0;
1628 
1629  enc->err_recognition = AV_EF_COMPLIANT;
1630  enc->workaround_bugs = FF_BUG_AUTODETECT;
1631  enc->error_concealment = FF_EC_GUESS_MVS | FF_EC_DEBLOCK;
1632  enc->idct_algo = FF_IDCT_AUTO;
1633  enc->debug = 0;
1634  // enc->error_rate = 0;
1635 
1636  const AVCodec *codec1 = enc->codec;
1637 
1638  if (selectedStream)
1639  {
1640  directrendering = true;
1641  }
1642 
1643  AVDictionaryEntry *metatag =
1644  av_dict_get(stream->metadata, "rotate", nullptr, 0);
1645  if (metatag && metatag->value && QString("180") == metatag->value)
1646  video_inverted = true;
1647 
1648 #ifdef USING_VDPAU
1650  {
1651  enc->get_buffer2 = get_avf_buffer_vdpau;
1652  enc->get_format = get_format_vdpau;
1653  enc->slice_flags = SLICE_FLAG_CODED_ORDER | SLICE_FLAG_ALLOW_FIELD;
1654  }
1655  else
1656 #endif
1657 #ifdef USING_DXVA2
1658  if (CODEC_IS_DXVA2(codec1, enc))
1659  {
1660  enc->get_buffer2 = get_avf_buffer_dxva2;
1661  enc->get_format = get_format_dxva2;
1662  }
1663  else
1664 #endif
1665 #ifdef USING_VAAPI
1666  if (CODEC_IS_VAAPI(codec1, enc) && codec_is_vaapi(video_codec_id))
1667  {
1668  enc->get_buffer2 = get_avf_buffer_vaapi;
1669  enc->get_format = get_format_vaapi;
1670  enc->slice_flags = SLICE_FLAG_CODED_ORDER | SLICE_FLAG_ALLOW_FIELD;
1671  }
1672  else
1673 #endif
1674 #ifdef USING_MEDIACODEC
1675  if (CODEC_IS_MEDIACODEC(codec1))
1676  {
1677  enc->get_format = get_format_mediacodec;
1678  enc->slice_flags = SLICE_FLAG_CODED_ORDER | SLICE_FLAG_ALLOW_FIELD;
1679  }
1680  else
1681 #endif
1682 #ifdef USING_VAAPI2
1684  {
1685  enc->get_buffer2 = get_avf_buffer_vaapi2;
1686  enc->get_format = get_format_vaapi2;
1687  }
1688  else
1689 #endif
1690  if (codec1 && codec1->capabilities & AV_CODEC_CAP_DR1)
1691  {
1692  // enc->flags |= CODEC_FLAG_EMU_EDGE;
1693  }
1694  else
1695  {
1696  if (selectedStream)
1697  directrendering = false;
1698  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1699  QString("Using software scaling to convert pixel format %1 for "
1700  "codec %2").arg(enc->pix_fmt)
1701  .arg(ff_codec_id_string(enc->codec_id)));
1702  }
1703 
1704  QString deinterlacer;
1705  if (m_mythcodecctx)
1706  deinterlacer = m_mythcodecctx->getDeinterlacerName();
1707  delete m_mythcodecctx;
1710  m_mythcodecctx->setStream(stream);
1711  m_mythcodecctx->setDeinterlacer(true,deinterlacer);
1712 
1713  int ret = m_mythcodecctx->HwDecoderInit(enc);
1714  if (ret < 0)
1715  {
1716  char error[AV_ERROR_MAX_STRING_SIZE];
1717  if (ret < 0)
1718  {
1719  LOG(VB_GENERAL, LOG_ERR, LOC +
1720  QString("HwDecoderInit unable to initialize hardware decoder: %1 (%2)")
1721  .arg(av_make_error_string(error, sizeof(error), ret))
1722  .arg(ret));
1723  // force it to switch to software decoding
1725  m_streams_changed = true;
1726  }
1727  }
1728 
1732  {
1733  if (codec1 &&
1734  ((AV_CODEC_ID_MPEG2VIDEO == codec1->id) ||
1735  (AV_CODEC_ID_MPEG1VIDEO == codec1->id)))
1736  {
1738  {
1739  uint total_blocks = (enc->height+15) / 16;
1740  enc->skip_top = (total_blocks+3) / 4;
1741  enc->skip_bottom = (total_blocks+3) / 4;
1742  }
1743 
1744  if (FlagIsSet(kDecodeLowRes))
1745  enc->lowres = 2; // 1 = 1/2 size, 2 = 1/4 size
1746  }
1747  else if (codec1 && (AV_CODEC_ID_H264 == codec1->id))
1748  {
1750  {
1751  enc->flags &= ~AV_CODEC_FLAG_LOOP_FILTER;
1752  enc->skip_loop_filter = AVDISCARD_ALL;
1753  }
1754  }
1755 
1757  {
1758  enc->skip_idct = AVDISCARD_ALL;
1759  }
1760  }
1761 
1762  if (selectedStream)
1763  {
1764  fps = normalized_fps(stream, enc);
1765  QSize dim = get_video_dim(*enc);
1766  int width = current_width = dim.width();
1767  int height = current_height = dim.height();
1768  current_aspect = get_aspect(*enc);
1769 
1770  if (!width || !height)
1771  {
1772  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1773  "InitVideoCodec invalid dimensions, resetting decoder.");
1774  width = 640;
1775  height = 480;
1776  fps = 29.97f;
1777  current_aspect = 4.0f / 3.0f;
1778  }
1779 
1781  const AVCodec *codec2 = enc->codec;
1782  QString codecName;
1783  if (codec2)
1784  codecName = codec2->name;
1785  m_parent->SetVideoParams(width, height, fps, kScan_Detect, codecName);
1786  if (LCD *lcd = LCD::Get())
1787  {
1788  LCDVideoFormatSet video_format;
1789 
1790  switch (enc->codec_id)
1791  {
1792  case AV_CODEC_ID_H263:
1793  case AV_CODEC_ID_MPEG4:
1794  case AV_CODEC_ID_MSMPEG4V1:
1795  case AV_CODEC_ID_MSMPEG4V2:
1796  case AV_CODEC_ID_MSMPEG4V3:
1797  case AV_CODEC_ID_H263P:
1798  case AV_CODEC_ID_H263I:
1799  video_format = VIDEO_DIVX;
1800  break;
1801  case AV_CODEC_ID_WMV1:
1802  case AV_CODEC_ID_WMV2:
1803  video_format = VIDEO_WMV;
1804  break;
1805 #if 0
1806  case AV_CODEC_ID_XVID:
1807  video_format = VIDEO_XVID;
1808  break;
1809 #endif
1810  default:
1811  video_format = VIDEO_MPG;
1812  break;
1813  }
1814 
1815  lcd->setVideoFormatLEDs(video_format, true);
1816 
1817  if(height >= 720)
1818  lcd->setVariousLEDs(VARIOUS_HDTV, true);
1819  else
1820  lcd->setVariousLEDs(VARIOUS_HDTV, false);
1821  }
1822  }
1823 }
1824 
1825 // CC Parity checking
1826 // taken from xine-lib libspucc
1827 
1828 static int cc608_parity(uint8_t byte)
1829 {
1830  int ones = 0;
1831 
1832  for (int i = 0; i < 7; i++)
1833  {
1834  if (byte & (1 << i))
1835  ones++;
1836  }
1837 
1838  return ones & 1;
1839 }
1840 
1841 // CC Parity checking
1842 // taken from xine-lib libspucc
1843 
1844 static void cc608_build_parity_table(int *parity_table)
1845 {
1846  for (uint8_t byte = 0; byte <= 127; byte++)
1847  {
1848  int parity_v = cc608_parity(byte);
1849  /* CC uses odd parity (i.e., # of 1's in byte is odd.) */
1850  parity_table[byte] = parity_v;
1851  parity_table[byte | 0x80] = !parity_v;
1852  }
1853 }
1854 
1855 // CC Parity checking
1856 // taken from xine-lib libspucc
1857 
1858 static int cc608_good_parity(const int *parity_table, uint16_t data)
1859 {
1860  int ret = parity_table[data & 0xff] && parity_table[(data & 0xff00) >> 8];
1861  if (!ret)
1862  {
1863  LOG(VB_VBI, LOG_ERR, LOC +
1864  QString("VBI: Bad parity in EIA-608 data (%1)") .arg(data,0,16));
1865  }
1866  return ret;
1867 }
1868 
1870 {
1871  memset(ccX08_in_pmt, 0, sizeof(ccX08_in_pmt));
1872  pmt_tracks.clear();
1873  pmt_track_types.clear();
1874 
1875  // Figure out languages of ATSC captions
1876  if (!ic->cur_pmt_sect)
1877  {
1878  LOG(VB_GENERAL, LOG_DEBUG, LOC +
1879  "ScanATSCCaptionStreams() called with no PMT");
1880  return;
1881  }
1882 
1883  const ProgramMapTable pmt(PSIPTable(ic->cur_pmt_sect));
1884 
1885  uint i;
1886  for (i = 0; i < pmt.StreamCount(); i++)
1887  {
1888  // MythTV remaps OpenCable Video to normal video during recording
1889  // so "dvb" is the safest choice for system info type, since this
1890  // will ignore other uses of the same stream id in DVB countries.
1891  if (pmt.IsVideo(i, "dvb"))
1892  break;
1893  }
1894 
1895  if (!pmt.IsVideo(i, "dvb"))
1896  return;
1897 
1899  pmt.StreamInfo(i), pmt.StreamInfoLength(i),
1901 
1902  const desc_list_t desc_list2 = MPEGDescriptor::ParseOnlyInclude(
1903  pmt.ProgramInfo(), pmt.ProgramInfoLength(),
1905 
1906  desc_list.insert(desc_list.end(), desc_list2.begin(), desc_list2.end());
1907 
1908  for (uint j = 0; j < desc_list.size(); j++)
1909  {
1910  const CaptionServiceDescriptor csd(desc_list[j]);
1911  for (uint k = 0; k < csd.ServicesCount(); k++)
1912  {
1913  int lang = csd.CanonicalLanguageKey(k);
1914  int type = csd.Type(k) ? 1 : 0;
1915  if (type)
1916  {
1917  StreamInfo si(av_index, lang, 0/*lang_idx*/,
1918  csd.CaptionServiceNumber(k),
1919  csd.EasyReader(k),
1920  csd.WideAspectRatio(k));
1921  uint key = csd.CaptionServiceNumber(k) + 4;
1922  ccX08_in_pmt[key] = true;
1923  pmt_tracks.push_back(si);
1924  pmt_track_types.push_back(kTrackTypeCC708);
1925  }
1926  else
1927  {
1928  int line21 = csd.Line21Field(k) ? 3 : 1;
1929  StreamInfo si(av_index, lang, 0/*lang_idx*/, line21, 0);
1930  ccX08_in_pmt[line21-1] = true;
1931  pmt_tracks.push_back(si);
1932  pmt_track_types.push_back(kTrackTypeCC608);
1933  }
1934  }
1935  }
1936 }
1937 
1939 {
1940  tracks[kTrackTypeCC608].clear();
1941  tracks[kTrackTypeCC708].clear();
1942  memset(ccX08_in_tracks, 0, sizeof(ccX08_in_tracks));
1943 
1944  uint pidx = 0, sidx = 0;
1945  map<int,uint> lang_cc_cnt[2];
1946  while (true)
1947  {
1948  bool pofr = pidx >= (uint)pmt_tracks.size();
1949  bool sofr = sidx >= (uint)stream_tracks.size();
1950  if (pofr && sofr)
1951  break;
1952 
1953  // choose lowest available next..
1954  // stream_id's of 608 and 708 streams alias, but this
1955  // is ok as we just want each list to be ordered.
1956  StreamInfo const *si = nullptr;
1957  int type = 0; // 0 if 608, 1 if 708
1958  bool isp = true; // if true use pmt_tracks next, else stream_tracks
1959 
1960  if (pofr && !sofr)
1961  isp = false;
1962  else if (!pofr && sofr)
1963  isp = true;
1964  else if (stream_tracks[sidx] < pmt_tracks[pidx])
1965  isp = false;
1966 
1967  if (isp)
1968  {
1969  si = &pmt_tracks[pidx];
1970  type = kTrackTypeCC708 == pmt_track_types[pidx] ? 1 : 0;
1971  pidx++;
1972  }
1973  else
1974  {
1975  si = &stream_tracks[sidx];
1976  type = kTrackTypeCC708 == stream_track_types[sidx] ? 1 : 0;
1977  sidx++;
1978  }
1979 
1980  StreamInfo nsi(*si);
1981  int lang_indx = lang_cc_cnt[type][nsi.language];
1982  lang_cc_cnt[type][nsi.language]++;
1983  nsi.language_index = lang_indx;
1984  tracks[(type) ? kTrackTypeCC708 : kTrackTypeCC608].push_back(nsi);
1985  int key = nsi.stream_id + ((type) ? 4 : -1);
1986  if (key < 0)
1987  {
1988  LOG(VB_GENERAL, LOG_ERR, LOC + "in_tracks key too small");
1989  }
1990  else
1991  {
1992  ccX08_in_tracks[key] = true;
1993  }
1994  LOG(VB_PLAYBACK, LOG_INFO, LOC +
1995  QString("%1 caption service #%2 is in the %3 language.")
1996  .arg((type) ? "EIA-708" : "EIA-608")
1997  .arg(nsi.stream_id)
1998  .arg(iso639_key_toName(nsi.language)));
1999  }
2000 }
2001 
2003 {
2004  // ScanStreams() calls tracks[kTrackTypeTeletextCaptions].clear()
2005  if (!ic->cur_pmt_sect || tracks[kTrackTypeTeletextCaptions].size())
2006  return;
2007 
2008  const ProgramMapTable pmt(PSIPTable(ic->cur_pmt_sect));
2009 
2010  for (uint i = 0; i < pmt.StreamCount(); i++)
2011  {
2012  if (pmt.StreamType(i) != StreamID::PrivData)
2013  continue;
2014 
2016  pmt.StreamInfo(i), pmt.StreamInfoLength(i),
2018 
2019  for (uint j = 0; j < desc_list.size(); j++)
2020  {
2021  const TeletextDescriptor td(desc_list[j]);
2022  for (uint k = 0; k < td.StreamCount(); k++)
2023  {
2024  int type = td.TeletextType(k);
2025  int language = td.CanonicalLanguageKey(k);
2026  int magazine = td.TeletextMagazineNum(k);
2027  if (magazine == 0)
2028  magazine = 8;
2029  int pagenum = td.TeletextPageNum(k);
2030  int lang_idx = (magazine << 8) | pagenum;
2031  StreamInfo si(av_index, language, lang_idx, 0, 0);
2032  if (type == 2 || type == 1)
2033  {
2034  TrackType track = (type == 2) ? kTrackTypeTeletextCaptions :
2036  tracks[track].push_back(si);
2037  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2038  QString("Teletext stream #%1 (%2) is in the %3 language"
2039  " on page %4 %5.")
2040  .arg(k).arg((type == 2) ? "Caption" : "Menu")
2041  .arg(iso639_key_toName(language))
2042  .arg(magazine).arg(pagenum));
2043  }
2044  }
2045  }
2046 
2047  // Assume there is only one multiplexed teletext stream in PMT..
2048  if (tracks[kTrackTypeTeletextCaptions].size())
2049  break;
2050  }
2051 }
2052 
2053 void AvFormatDecoder::ScanRawTextCaptions(int av_stream_index)
2054 {
2055  AVDictionaryEntry *metatag =
2056  av_dict_get(ic->streams[av_stream_index]->metadata, "language", nullptr,
2057  0);
2058  bool forced =
2059  ic->streams[av_stream_index]->disposition & AV_DISPOSITION_FORCED;
2060  int lang = metatag ? get_canonical_lang(metatag->value) :
2061  iso639_str3_to_key("und");
2062  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2063  QString("Text Subtitle track #%1 is A/V stream #%2 "
2064  "and is in the %3 language(%4), forced=%5.")
2065  .arg(tracks[kTrackTypeRawText].size()).arg(av_stream_index)
2066  .arg(iso639_key_toName(lang)).arg(lang).arg(forced));
2067  StreamInfo si(av_stream_index, lang, 0, 0, 0, false, false, forced);
2068  tracks[kTrackTypeRawText].push_back(si);
2069 }
2070 
2076 {
2077  if (!ic || !ic->cur_pmt_sect)
2078  return;
2079 
2080  if (!itv && ! (itv = m_parent->GetInteractiveTV()))
2081  return;
2082 
2083  const ProgramMapTable pmt(PSIPTable(ic->cur_pmt_sect));
2084 
2085  for (uint i = 0; i < pmt.StreamCount(); i++)
2086  {
2087  if (! StreamID::IsObjectCarousel(pmt.StreamType(i)))
2088  continue;
2089 
2090  LOG(VB_DSMCC, LOG_NOTICE, QString("ScanDSMCCStreams Found Object Carousel in Stream %1").arg(QString::number(i)));
2091 
2093  pmt.StreamInfo(i), pmt.StreamInfoLength(i),
2095 
2096  for (uint j = 0; j < desc_list.size(); j++)
2097  {
2098  const unsigned char *desc = desc_list[j];
2099  desc++; // Skip tag
2100  uint length = *desc++;
2101  const unsigned char *endDesc = desc+length;
2102  uint dataBroadcastId = desc[0]<<8 | desc[1];
2103  LOG(VB_DSMCC, LOG_NOTICE, QString("ScanDSMCCStreams dataBroadcastId %1").arg(QString::number(dataBroadcastId)));
2104  if (dataBroadcastId != 0x0106) // ETSI/UK Profile
2105  continue;
2106  desc += 2; // Skip data ID
2107  while (desc != endDesc)
2108  {
2109  uint appTypeCode = desc[0]<<8 | desc[1];
2110  desc += 3; // Skip app type code and boot priority hint
2111  uint appSpecDataLen = *desc++;
2112 #ifdef USING_MHEG
2113  LOG(VB_DSMCC, LOG_NOTICE, QString("ScanDSMCCStreams AppTypeCode %1").arg(QString::number(appTypeCode)));
2114  if (appTypeCode == 0x101) // UK MHEG profile
2115  {
2116  const unsigned char *subDescEnd = desc + appSpecDataLen;
2117  while (desc < subDescEnd)
2118  {
2119  uint sub_desc_tag = *desc++;
2120  uint sub_desc_len = *desc++;
2121  // Network boot info sub-descriptor.
2122  if (sub_desc_tag == 1)
2123  itv->SetNetBootInfo(desc, sub_desc_len);
2124  desc += sub_desc_len;
2125  }
2126  }
2127  else
2128 #else
2129  (void) appTypeCode;
2130 #endif // USING_MHEG
2131  {
2132  desc += appSpecDataLen;
2133  }
2134  }
2135  }
2136  }
2137 }
2138 
2140 {
2141  bool unknownbitrate = false;
2142  int scanerror = 0;
2143  bitrate = 0;
2144 
2145  tracks[kTrackTypeAttachment].clear();
2146  tracks[kTrackTypeAudio].clear();
2147  tracks[kTrackTypeSubtitle].clear();
2149  tracks[kTrackTypeTeletextMenu].clear();
2150  tracks[kTrackTypeRawText].clear();
2151  if (!novideo)
2152  {
2153  // we will rescan video streams
2154  tracks[kTrackTypeVideo].clear();
2156  fps = 0;
2157  }
2158  map<int,uint> lang_sub_cnt;
2159  uint subtitleStreamCount = 0;
2160  map<int,uint> lang_aud_cnt;
2161  uint audioStreamCount = 0;
2162 
2163  if (ringBuffer && ringBuffer->IsDVD() &&
2165  {
2166  ringBuffer->DVD()->AudioStreamsChanged(false);
2168  }
2169 
2170  for (uint strm = 0; strm < ic->nb_streams; strm++)
2171  {
2172  AVCodecParameters *par = ic->streams[strm]->codecpar;
2173  AVCodecContext *enc = nullptr;
2174 
2175  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2176  QString("Stream #%1, has id 0x%2 codec id %3, "
2177  "type %4, bitrate %5 at 0x%6")
2178  .arg(strm).arg((uint64_t)ic->streams[strm]->id,0,16)
2179  .arg(ff_codec_id_string(par->codec_id))
2180  .arg(ff_codec_type_string(par->codec_type))
2181  .arg(par->bit_rate).arg((uint64_t)ic->streams[strm],0,16));
2182 
2183  switch (par->codec_type)
2184  {
2185  case AVMEDIA_TYPE_VIDEO:
2186  {
2187  if (!par->codec_id)
2188  {
2189  LOG(VB_GENERAL, LOG_ERR, LOC +
2190  QString("Stream #%1 has an unknown video "
2191  "codec id, skipping.").arg(strm));
2192  continue;
2193  }
2194 
2195  // ffmpeg does not return a bitrate for several codecs and
2196  // formats. Forcing it to 500000 ensures the ringbuffer does not
2197  // use optimisations for low bitrate (audio and data) streams.
2198  if (par->bit_rate == 0)
2199  {
2200  par->bit_rate = 500000;
2201  unknownbitrate = true;
2202  }
2203  bitrate += par->bit_rate;
2204 
2205  break;
2206  }
2207  case AVMEDIA_TYPE_AUDIO:
2208  {
2209  enc = gCodecMap->hasCodecContext(ic->streams[strm]);
2210  if (enc && enc->internal)
2211  {
2212  LOG(VB_GENERAL, LOG_WARNING, LOC +
2213  QString("Warning, audio codec 0x%1 id(%2) "
2214  "type (%3) already open, leaving it alone.")
2215  .arg((uint64_t)enc,0,16)
2216  .arg(ff_codec_id_string(enc->codec_id))
2217  .arg(ff_codec_type_string(enc->codec_type)));
2218  }
2219  LOG(VB_GENERAL, LOG_INFO, LOC +
2220  QString("codec %1 has %2 channels")
2221  .arg(ff_codec_id_string(par->codec_id))
2222  .arg(par->channels));
2223 
2224  bitrate += par->bit_rate;
2225  break;
2226  }
2227  case AVMEDIA_TYPE_SUBTITLE:
2228  {
2229  if (par->codec_id == AV_CODEC_ID_DVB_TELETEXT)
2230  ScanTeletextCaptions(strm);
2231  if (par->codec_id == AV_CODEC_ID_TEXT)
2232  ScanRawTextCaptions(strm);
2233  bitrate += par->bit_rate;
2234 
2235  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("subtitle codec (%1)")
2236  .arg(ff_codec_type_string(par->codec_type)));
2237  break;
2238  }
2239  case AVMEDIA_TYPE_DATA:
2240  {
2241  ScanTeletextCaptions(strm);
2242  bitrate += par->bit_rate;
2243  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("data codec (%1)")
2244  .arg(ff_codec_type_string(par->codec_type)));
2245  break;
2246  }
2247  case AVMEDIA_TYPE_ATTACHMENT:
2248  {
2249  if (par->codec_id == AV_CODEC_ID_TTF)
2250  tracks[kTrackTypeAttachment].push_back(
2251  StreamInfo(strm, 0, 0, ic->streams[strm]->id, 0));
2252  bitrate += par->bit_rate;
2253  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2254  QString("Attachment codec (%1)")
2255  .arg(ff_codec_type_string(par->codec_type)));
2256  break;
2257  }
2258  default:
2259  {
2260  bitrate += par->bit_rate;
2261  LOG(VB_PLAYBACK, LOG_ERR, LOC +
2262  QString("Unknown codec type (%1)")
2263  .arg(ff_codec_type_string(par->codec_type)));
2264  break;
2265  }
2266  }
2267 
2268  if (par->codec_type != AVMEDIA_TYPE_AUDIO &&
2269  par->codec_type != AVMEDIA_TYPE_SUBTITLE)
2270  continue;
2271 
2272  // skip DVB teletext and text subs, there is no libavcodec decoder
2273  if (par->codec_type == AVMEDIA_TYPE_SUBTITLE &&
2274  (par->codec_id == AV_CODEC_ID_DVB_TELETEXT ||
2275  par->codec_id == AV_CODEC_ID_TEXT))
2276  continue;
2277 
2278  LOG(VB_PLAYBACK, LOG_INFO, LOC + QString("Looking for decoder for %1")
2279  .arg(ff_codec_id_string(par->codec_id)));
2280 
2281  if (par->codec_id == AV_CODEC_ID_PROBE)
2282  {
2283  LOG(VB_GENERAL, LOG_ERR, LOC +
2284  QString("Probing of stream #%1 unsuccesful, ignoring.").arg(strm));
2285  continue;
2286  }
2287 
2288  if (!enc)
2289  enc = gCodecMap->getCodecContext(ic->streams[strm]);
2290 
2291  const AVCodec *codec = nullptr;
2292  if (enc)
2293  codec = enc->codec;
2294  else
2295  {
2296  LOG(VB_GENERAL, LOG_ERR, LOC +
2297  QString("Could not find decoder for codec (%1), ignoring.")
2298  .arg(ff_codec_id_string(par->codec_id)));
2299 
2300  // Nigel's bogus codec-debug. Dump the list of codecs & decoders,
2301  // and have one last attempt to find a decoder. This is usually
2302  // only caused by build problems, where libavcodec needs a rebuild
2303  if (VERBOSE_LEVEL_CHECK(VB_LIBAV, LOG_ANY))
2304  {
2305  AVCodec *p = av_codec_next(nullptr);
2306  int i = 1;
2307  while (p)
2308  {
2309  QString msg;
2310 
2311  if (p->name[0] != '\0')
2312  msg = QString("Codec %1:").arg(p->name);
2313  else
2314  msg = QString("Codec %1, null name,").arg(strm);
2315 
2316  if (p->decode == nullptr)
2317  msg += "decoder is null";
2318 
2319  LOG(VB_LIBAV, LOG_INFO, LOC + msg);
2320 
2321  if (p->id == par->codec_id)
2322  {
2323  codec = p;
2324  break;
2325  }
2326 
2327  LOG(VB_LIBAV, LOG_INFO, LOC +
2328  QString("Codec 0x%1 != 0x%2") .arg(p->id, 0, 16)
2329  .arg(par->codec_id, 0, 16));
2330  p = av_codec_next(p);
2331  ++i;
2332  }
2333  }
2334  if (codec)
2335  enc = gCodecMap->getCodecContext(ic->streams[strm], codec);
2336  else
2337  continue;
2338  }
2339  if (!enc)
2340  continue;
2341 
2342  if (enc->codec && par->codec_id != enc->codec_id)
2343  {
2344  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2345  QString("Already opened codec not matching (%1 vs %2). Reopening")
2346  .arg(ff_codec_id_string(enc->codec_id))
2347  .arg(ff_codec_id_string(enc->codec->id)));
2348  gCodecMap->freeCodecContext(ic->streams[strm]);
2349  enc = gCodecMap->getCodecContext(ic->streams[strm]);
2350  }
2351  if (!OpenAVCodec(enc, codec))
2352  continue;
2353  if (!enc)
2354  continue;
2355 
2356  if (!IsValidStream(ic->streams[strm]->id))
2357  {
2358  /* Hide this stream if it's not valid in this context.
2359  * This can happen, for example, on a Blu-ray disc if there
2360  * are more physical streams than there is metadata about them.
2361  * (e.g. Despicable Me)
2362  */
2363  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2364  QString("Stream 0x%1 is not valid in this context - skipping")
2365  .arg(ic->streams[strm]->id, 4, 16));
2366  continue;
2367  }
2368 
2369  if (par->codec_type == AVMEDIA_TYPE_SUBTITLE)
2370  {
2371  bool forced = ic->streams[strm]->disposition & AV_DISPOSITION_FORCED;
2372  int lang = GetSubtitleLanguage(subtitleStreamCount, strm);
2373  int lang_indx = lang_sub_cnt[lang]++;
2374  subtitleStreamCount++;
2375 
2376  tracks[kTrackTypeSubtitle].push_back(
2377  StreamInfo(strm, lang, lang_indx, ic->streams[strm]->id, 0, 0, false, false, forced));
2378 
2379  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2380  QString("Subtitle track #%1 is A/V stream #%2 "
2381  "and is in the %3 language(%4).")
2382  .arg(tracks[kTrackTypeSubtitle].size()).arg(strm)
2383  .arg(iso639_key_toName(lang)).arg(lang));
2384  }
2385 
2386  if (par->codec_type == AVMEDIA_TYPE_AUDIO)
2387  {
2388  int lang = GetAudioLanguage(audioStreamCount, strm);
2390  int channels = par->channels;
2391  int lang_indx = lang_aud_cnt[lang]++;
2392  audioStreamCount++;
2393 
2394  if (enc->avcodec_dual_language)
2395  {
2396  tracks[kTrackTypeAudio].push_back(
2397  StreamInfo(strm, lang, lang_indx, ic->streams[strm]->id, channels,
2398  false, false, false, type));
2399  lang_indx = lang_aud_cnt[lang]++;
2400  tracks[kTrackTypeAudio].push_back(
2401  StreamInfo(strm, lang, lang_indx, ic->streams[strm]->id, channels,
2402  true, false, false, type));
2403  }
2404  else
2405  {
2406  int logical_stream_id;
2407  if (ringBuffer && ringBuffer->IsDVD())
2408  {
2409  logical_stream_id = ringBuffer->DVD()->GetAudioTrackNum(ic->streams[strm]->id);
2410  channels = ringBuffer->DVD()->GetNumAudioChannels(logical_stream_id);
2411  }
2412  else
2413  logical_stream_id = ic->streams[strm]->id;
2414 
2415  if (logical_stream_id == -1)
2416  {
2417  // This stream isn't mapped, so skip it
2418  continue;
2419  }
2420 
2421  tracks[kTrackTypeAudio].push_back(
2422  StreamInfo(strm, lang, lang_indx, logical_stream_id, channels,
2423  false, false, false, type));
2424  }
2425 
2426  LOG(VB_AUDIO, LOG_INFO, LOC +
2427  QString("Audio Track #%1, of type (%2) is A/V stream #%3 (id=0x%4) "
2428  "and has %5 channels in the %6 language(%7).")
2429  .arg(tracks[kTrackTypeAudio].size()).arg(toString(type))
2430  .arg(strm).arg(ic->streams[strm]->id,0,16).arg(enc->channels)
2431  .arg(iso639_key_toName(lang)).arg(lang));
2432  }
2433  }
2434 
2435  // Now find best video track to play
2436  if (!novideo && ic)
2437  {
2438  for(;;)
2439  {
2440  AVCodec *codec = nullptr;
2441  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2442  "Trying to select best video track");
2443 
2444  /*
2445  * Find the "best" stream in the file.
2446  *
2447  * The best stream is determined according to various heuristics as
2448  * the most likely to be what the user expects. If the decoder parameter
2449  * is not nullptr, av_find_best_stream will find the default decoder
2450  * for the stream's codec; streams for which no decoder can be found
2451  * are ignored.
2452  *
2453  * If av_find_best_stream returns successfully and decoder_ret is not nullptr,
2454  * then *decoder_ret is guaranteed to be set to a valid AVCodec.
2455  */
2456  int selTrack = av_find_best_stream(ic, AVMEDIA_TYPE_VIDEO,
2457  -1, -1, &codec, 0);
2458 
2459  if (selTrack < 0)
2460  {
2461  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2462  "No video track found/selected.");
2463  break;
2464  }
2465 
2467  gCodecMap->freeCodecContext(ic->streams[selTrack]);
2468  AVCodecContext *enc = gCodecMap->getCodecContext(ic->streams[selTrack], codec);
2469  StreamInfo si(selTrack, 0, 0, 0, 0);
2470 
2471  tracks[kTrackTypeVideo].push_back(si);
2473 
2474  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2475  QString("Selected track #%1 (id 0x%2 codec id %3, "
2476  "type %4, bitrate %5 at 0x%6)")
2477  .arg(selTrack).arg((uint64_t)ic->streams[selTrack]->id,0,16)
2478  .arg(ff_codec_id_string(enc->codec_id))
2479  .arg(ff_codec_type_string(enc->codec_type))
2480  .arg(enc->bit_rate).arg((uint64_t)ic->streams[selTrack],0,16));
2481 
2482  codec_is_mpeg = CODEC_IS_FFMPEG_MPEG(enc->codec_id);
2483 
2484  delete private_dec;
2485  private_dec = nullptr;
2486  m_h264_parser->Reset();
2487 
2488  QSize dim = get_video_dim(*enc);
2489  uint width = max(dim.width(), 16);
2490  uint height = max(dim.height(), 16);
2491  QString dec = "ffmpeg";
2492  uint thread_count = 1;
2493  QString codecName;
2494  if (enc->codec)
2495  codecName = enc->codec->name;
2496  if (enc->framerate.den && enc->framerate.num)
2497  fps = float(enc->framerate.num) / float(enc->framerate.den);
2498  else
2499  fps = 0;
2500  if (!is_db_ignored)
2501  {
2502  VideoDisplayProfile vdp;
2503  vdp.SetInput(QSize(width, height),fps,codecName);
2504  dec = vdp.GetDecoder();
2505  thread_count = vdp.GetMaxCPUs();
2506  bool skip_loop_filter = vdp.IsSkipLoopEnabled();
2507  if (!skip_loop_filter)
2508  {
2509  enc->skip_loop_filter = AVDISCARD_NONKEY;
2510  }
2511  }
2512 
2514  int version = mpeg_version(enc->codec_id);
2515  if (version)
2517 
2518  // Check it's a codec we can decode using GPU
2519 #ifdef USING_OPENMAX
2520  // The OpenMAX decoder supports H264 high 10, 422 and 444 profiles
2521  if (dec != "openmax")
2522 #endif
2524  {
2525  bool wasgpu = dec != "ffmpeg";
2526  if (FlagIsSet(kDecodeAllowGPU) && force_sw_decode(enc) && wasgpu)
2527  {
2528  LOG(VB_GENERAL, LOG_WARNING, LOC +
2529  "Unsupported Video Profile - forcing software decode");
2530  }
2532  {
2533  LOG(VB_GENERAL, LOG_WARNING, LOC +
2534  "GPU decoding failed - forcing software decode");
2535  }
2536  averror_count = 0;
2537  dec = "ffmpeg";
2538  }
2539 
2541  {
2542  bool foundgpudecoder = false;
2543 
2544 #ifdef USING_VDPAU
2545  MythCodecID vdpau_mcid;
2546  vdpau_mcid =
2547  VideoOutputVDPAU::GetBestSupportedCodec(width, height, dec,
2548  mpeg_version(enc->codec_id),
2549  false);
2550 
2551  if (codec_is_vdpau(vdpau_mcid))
2552  {
2553  video_codec_id = vdpau_mcid;
2554  foundgpudecoder = true;
2555  }
2556 #endif // USING_VDPAU
2557 #ifdef USING_GLVAAPI
2558  if (!foundgpudecoder)
2559  {
2560  MythCodecID vaapi_mcid;
2561  AVPixelFormat pix_fmt = AV_PIX_FMT_YUV420P;
2562  vaapi_mcid =
2564  mpeg_version(enc->codec_id),
2565  false,
2566  pix_fmt);
2567 
2568  if (codec_is_vaapi(vaapi_mcid))
2569  {
2570  video_codec_id = vaapi_mcid;
2571  enc->pix_fmt = pix_fmt;
2572  foundgpudecoder = true;
2573  }
2574  }
2575 #endif // USING_GLVAAPI
2576 #ifdef USING_DXVA2
2577  if (!foundgpudecode)
2578  {
2579  MythCodecID dxva2_mcid;
2580  AVPixelFormat pix_fmt = AV_PIX_FMT_YUV420P;
2582  width, height, dec, mpeg_version(enc->codec_id),
2583  false, pix_fmt);
2584 
2585  if (codec_is_dxva2(dxva2_mcid))
2586  {
2587  video_codec_id = dxva2_mcid;
2588  enc->pix_fmt = pix_fmt;
2589  foundgpudecoder = true;
2590  }
2591  }
2592 #endif // USING_DXVA2
2593 #ifdef USING_MEDIACODEC
2594  if (!foundgpudecoder)
2595  {
2596  MythCodecID mediacodec_mcid;
2597  AVPixelFormat pix_fmt = AV_PIX_FMT_YUV420P;
2598  mediacodec_mcid = MediaCodecContext::GetBestSupportedCodec(
2599  &codec, dec, mpeg_version(enc->codec_id),
2600  pix_fmt);
2601 
2602  if (codec_is_mediacodec(mediacodec_mcid))
2603  {
2604  gCodecMap->freeCodecContext(ic->streams[selTrack]);
2605  enc = gCodecMap->getCodecContext(ic->streams[selTrack], codec);
2606  video_codec_id = mediacodec_mcid;
2607  foundgpudecoder = true;
2608  }
2609  }
2610 #endif // USING_MEDIACODEC
2611 #ifdef USING_VAAPI2
2612  if (!foundgpudecoder)
2613  {
2614  MythCodecID vaapi2_mcid;
2615  AVPixelFormat pix_fmt = AV_PIX_FMT_YUV420P;
2617  &codec, dec, mpeg_version(enc->codec_id),
2618  pix_fmt);
2619 
2620  if (codec_is_vaapi2(vaapi2_mcid))
2621  {
2622  gCodecMap->freeCodecContext(ic->streams[selTrack]);
2623  enc = gCodecMap->getCodecContext(ic->streams[selTrack], codec);
2624  video_codec_id = vaapi2_mcid;
2625  foundgpudecoder = true;
2626  }
2627  }
2628 #endif // USING_VAAPI2
2629  }
2630  // default to mpeg2
2631  if (video_codec_id == kCodec_NONE)
2632  {
2633  LOG(VB_GENERAL, LOG_ERR, LOC +
2634  "Unknown video codec - defaulting to MPEG2");
2636  }
2637 
2638  // Use a PrivateDecoder if allowed in playerFlags AND matched
2639  // via the decoder name
2641  if (private_dec)
2642  thread_count = 1;
2643 
2644  use_frame_timing = false;
2645  if (! ringBuffer->IsDVD()
2649  || GetCodecDecoderName() == "openmax"))
2650  use_frame_timing = true;
2651 
2653  thread_count = 1;
2654 
2655  LOG(VB_PLAYBACK, LOG_INFO, LOC +
2656  QString("Using %1 CPUs for decoding")
2657  .arg(HAVE_THREADS ? thread_count : 1));
2658 
2659  if (HAVE_THREADS)
2660  enc->thread_count = thread_count;
2661 
2662  InitVideoCodec(ic->streams[selTrack], enc, true);
2663 
2664  ScanATSCCaptionStreams(selTrack);
2666 
2667  LOG(VB_GENERAL, LOG_INFO, LOC +
2668  QString("Using %1 for video decoding")
2669  .arg(GetCodecDecoderName()));
2670 
2671  {
2672  QMutexLocker locker(avcodeclock);
2673 
2674  if (!OpenAVCodec(enc, codec))
2675  {
2676  scanerror = -1;
2677  break;
2678  }
2679  }
2680 
2681  break;
2682  }
2683  }
2684 
2685  if (ic && ((uint)ic->bit_rate > bitrate))
2686  bitrate = (uint)ic->bit_rate;
2687 
2688  if (bitrate > 0)
2689  {
2690  bitrate = (bitrate + 999) / 1000;
2691  if (ringBuffer)
2693  }
2694 
2695  // update RingBuffer buffer size
2696  if (ringBuffer)
2697  {
2698  ringBuffer->SetBufferSizeFactors(unknownbitrate,
2699  ic && QString(ic->iformat->name).contains("matroska"));
2700  }
2701 
2703 
2704  // Select a new track at the next opportunity.
2705  ResetTracks();
2706 
2707  // We have to do this here to avoid the NVP getting stuck
2708  // waiting on audio.
2709  if (m_audio->HasAudioIn() && tracks[kTrackTypeAudio].empty())
2710  {
2711  m_audio->SetAudioParams(FORMAT_NONE, -1, -1, AV_CODEC_ID_NONE, -1, false);
2712  m_audio->ReinitAudio();
2713  if (ringBuffer && ringBuffer->IsDVD())
2714  audioIn = AudioInfo();
2715  }
2716 
2717  // if we don't have a video stream we still need to make sure some
2718  // video params are set properly
2719  if (selectedTrack[kTrackTypeVideo].av_stream_index == -1)
2720  {
2721  QString tvformat = gCoreContext->GetSetting("TVFormat").toLower();
2722  if (tvformat == "ntsc" || tvformat == "ntsc-jp" ||
2723  tvformat == "pal-m" || tvformat == "atsc")
2724  {
2725  fps = 29.97;
2726  m_parent->SetVideoParams(-1, -1, 29.97);
2727  }
2728  else
2729  {
2730  fps = 25.0;
2731  m_parent->SetVideoParams(-1, -1, 25.0);
2732  }
2733  }
2734 
2735  if (m_parent->IsErrored())
2736  scanerror = -1;
2737 
2738  ScanDSMCCStreams();
2739 
2740  return scanerror;
2741 }
2742 
2743 bool AvFormatDecoder::OpenAVCodec(AVCodecContext *avctx, const AVCodec *codec)
2744 {
2745  QMutexLocker locker(avcodeclock);
2746 
2747 #ifdef USING_MEDIACODEC
2748  if (QString("mediacodec") == codec->wrapper_name)
2749  av_jni_set_java_vm(QAndroidJniEnvironment::javaVM(), nullptr);
2750 #endif
2751  int ret = avcodec_open2(avctx, codec, nullptr);
2752  if (ret < 0)
2753  {
2754  char error[AV_ERROR_MAX_STRING_SIZE];
2755 
2756  av_make_error_string(error, sizeof(error), ret);
2757  LOG(VB_GENERAL, LOG_ERR, LOC +
2758  QString("Could not open codec 0x%1, id(%2) type(%3) "
2759  "ignoring. reason %4").arg((uint64_t)avctx,0,16)
2760  .arg(ff_codec_id_string(avctx->codec_id))
2761  .arg(ff_codec_type_string(avctx->codec_type))
2762  .arg(error));
2763  return false;
2764  }
2765  else
2766  {
2767  LOG(VB_GENERAL, LOG_INFO, LOC +
2768  QString("Opened codec 0x%1, id(%2) type(%3)")
2769  .arg((uint64_t)avctx,0,16)
2770  .arg(ff_codec_id_string(avctx->codec_id))
2771  .arg(ff_codec_type_string(avctx->codec_type)));
2772  return true;
2773  }
2774 }
2775 
2777 {
2779 }
2780 
2781 bool AvFormatDecoder::DoRewindSeek(long long desiredFrame)
2782 {
2783  return DecoderBase::DoRewindSeek(desiredFrame);
2784 }
2785 
2786 void AvFormatDecoder::DoFastForwardSeek(long long desiredFrame, bool &needflush)
2787 {
2788  DecoderBase::DoFastForwardSeek(desiredFrame, needflush);
2789  return;
2790 }
2793 {
2794  for (uint i = 0; i < (uint) tracks[kTrackTypeTeletextCaptions].size(); i++)
2795  {
2796  if (tracks[kTrackTypeTeletextCaptions][i].language_index == lang_idx)
2797  {
2798  return tracks[kTrackTypeTeletextCaptions][i].language;
2799  }
2800  }
2801 
2802  return iso639_str3_to_key("und");
2803 }
2805 int AvFormatDecoder::GetSubtitleLanguage(uint subtitle_index, uint stream_index)
2806 {
2807  (void)subtitle_index;
2808  AVDictionaryEntry *metatag =
2809  av_dict_get(ic->streams[stream_index]->metadata, "language", nullptr, 0);
2810  return metatag ? get_canonical_lang(metatag->value) :
2811  iso639_str3_to_key("und");
2812 }
2813 
2815 int AvFormatDecoder::GetCaptionLanguage(TrackTypes trackType, int service_num)
2816 {
2817  int ret = -1;
2818  for (uint i = 0; i < (uint) pmt_track_types.size(); i++)
2819  {
2820  if ((pmt_track_types[i] == trackType) &&
2821  (pmt_tracks[i].stream_id == service_num))
2822  {
2823  ret = pmt_tracks[i].language;
2824  if (!iso639_is_key_undefined(ret))
2825  return ret;
2826  }
2827  }
2828 
2829  for (uint i = 0; i < (uint) stream_track_types.size(); i++)
2830  {
2831  if ((stream_track_types[i] == trackType) &&
2832  (stream_tracks[i].stream_id == service_num))
2833  {
2834  ret = stream_tracks[i].language;
2835  if (!iso639_is_key_undefined(ret))
2836  return ret;
2837  }
2838  }
2839 
2840  return ret;
2841 }
2842 
2843 int AvFormatDecoder::GetAudioLanguage(uint audio_index, uint stream_index)
2844 {
2845  return GetSubtitleLanguage(audio_index, stream_index);
2846 }
2847 
2849 {
2851  AVStream *stream = ic->streams[stream_index];
2852 
2853  if (ic->cur_pmt_sect) // mpeg-ts
2854  {
2855  const ProgramMapTable pmt(PSIPTable(ic->cur_pmt_sect));
2856  switch (pmt.GetAudioType(stream_index))
2857  {
2858  case 0x01 :
2860  break;
2861  case 0x02 :
2863  break;
2864  case 0x03 :
2866  break;
2867  case 0x00 :
2868  default:
2870  }
2871  }
2872  else // all other containers
2873  {
2874  // We only support labelling/filtering of these two types for now
2875  if (stream->disposition & AV_DISPOSITION_VISUAL_IMPAIRED)
2877  else if (stream->disposition & AV_DISPOSITION_COMMENT)
2879  else if (stream->disposition & AV_DISPOSITION_HEARING_IMPAIRED)
2881  else if (stream->disposition & AV_DISPOSITION_CLEAN_EFFECTS)
2883  }
2884 
2885  return type;
2886 }
2887 
2901 {
2902  QMutexLocker locker(avcodeclock);
2903 
2904  // Find the position of the streaminfo in tracks[kTrackTypeAudio]
2905  sinfo_vec_t::iterator current = tracks[kTrackTypeAudio].begin();
2906  for (; current != tracks[kTrackTypeAudio].end(); ++current)
2907  {
2908  if (current->av_stream_index == streamIndex)
2909  break;
2910  }
2911 
2912  if (current == tracks[kTrackTypeAudio].end())
2913  {
2914  LOG(VB_GENERAL, LOG_WARNING, LOC +
2915  QString("Invalid stream index passed to "
2916  "SetupAudioStreamSubIndexes: %1").arg(streamIndex));
2917 
2918  return;
2919  }
2920 
2921  // Remove the extra substream or duplicate the current substream
2922  sinfo_vec_t::iterator next = current + 1;
2923  if (current->av_substream_index == -1)
2924  {
2925  // Split stream in two (Language I + Language II)
2926  StreamInfo lang1 = *current;
2927  StreamInfo lang2 = *current;
2928  lang1.av_substream_index = 0;
2929  lang2.av_substream_index = 1;
2930  *current = lang1;
2931  tracks[kTrackTypeAudio].insert(next, lang2);
2932  return;
2933  }
2934 
2935  if ((next == tracks[kTrackTypeAudio].end()) ||
2936  (next->av_stream_index != streamIndex))
2937  {
2938  QString msg = QString(
2939  "Expected substream 1 (Language I) of stream %1\n\t\t\t"
2940  "following substream 0, found end of list or another stream.")
2941  .arg(streamIndex);
2942 
2943  LOG(VB_GENERAL, LOG_WARNING, LOC + msg);
2944 
2945  return;
2946  }
2947 
2948  // Remove extra stream info
2949  StreamInfo stream = *current;
2950  stream.av_substream_index = -1;
2951  *current = stream;
2952  tracks[kTrackTypeAudio].erase(next);
2953 }
2954 
2960 {
2961  if (!m_audio->HasAudioIn())
2962  return;
2963 
2964  QMutexLocker locker(avcodeclock);
2965  for (uint i = 0; i < ic->nb_streams;)
2966  {
2967  AVStream *st = ic->streams[i];
2968  AVCodecContext *avctx = gCodecMap->hasCodecContext(st);
2969  if (avctx && avctx->codec_type == AVMEDIA_TYPE_AUDIO)
2970  {
2972  av_remove_stream(ic, st->id, 0);
2973  i--;
2974  }
2975  else
2976  i++;
2977  }
2978 }
2979 
2980 int get_avf_buffer(struct AVCodecContext *c, AVFrame *pic, int flags)
2981 {
2982  AvFormatDecoder *nd = (AvFormatDecoder *)(c->opaque);
2983 
2984  if (!IS_DR1_PIX_FMT(c->pix_fmt))
2985  {
2986  nd->directrendering = false;
2987  return avcodec_default_get_buffer2(c, pic, flags);
2988  }
2989  nd->directrendering = true;
2990 
2991  VideoFrame *frame = nd->GetPlayer()->GetNextVideoFrame();
2992 
2993  if (!frame)
2994  return -1;
2995 
2996  for (int i = 0; i < 3; i++)
2997  {
2998  pic->data[i] = frame->buf + frame->offsets[i];
2999  pic->linesize[i] = frame->pitches[i];
3000  }
3001 
3002  pic->opaque = frame;
3003  pic->reordered_opaque = c->reordered_opaque;
3004 
3005  // Set release method
3006  AVBufferRef *buffer =
3007  av_buffer_create((uint8_t*)frame, 0, release_avf_buffer, nd, 0);
3008  pic->buf[0] = buffer;
3009 
3010  return 0;
3011 }
3012 
3013 void release_avf_buffer(void *opaque, uint8_t *data)
3014 {
3015  AvFormatDecoder *nd = (AvFormatDecoder *)opaque;
3016  VideoFrame *frame = (VideoFrame*)data;
3017 
3018  if (nd && nd->GetPlayer())
3019  nd->GetPlayer()->DeLimboFrame(frame);
3020 }
3021 
3022 #ifdef USING_VAAPI2
3023 static void dummy_release_avf_buffer(void * /*opaque*/, uint8_t * /*data*/)
3024 {
3025 }
3026 #endif
3027 
3028 #ifdef USING_VDPAU
3029 int get_avf_buffer_vdpau(struct AVCodecContext *c, AVFrame *pic, int /*flags*/)
3030 {
3031  AvFormatDecoder *nd = (AvFormatDecoder *)(c->opaque);
3032  VideoFrame *frame = nd->GetPlayer()->GetNextVideoFrame();
3033 
3034  struct vdpau_render_state *render = (struct vdpau_render_state *)frame->buf;
3036 
3037  for (int i = 0; i < 4; i++)
3038  {
3039  pic->data[i] = nullptr;
3040  pic->linesize[i] = 0;
3041  }
3042  pic->opaque = frame;
3043  pic->data[3] = (uint8_t*)(uintptr_t)render->surface;
3044  frame->pix_fmt = c->pix_fmt;
3045  pic->reordered_opaque = c->reordered_opaque;
3046 
3047  // Set release method
3048  AVBufferRef *buffer =
3049  av_buffer_create((uint8_t*)frame, 0, release_avf_buffer_vdpau, nd, 0);
3050  pic->buf[0] = buffer;
3051 
3052  return 0;
3053 }
3054 
3055 void release_avf_buffer_vdpau(void *opaque, uint8_t *data)
3056 {
3057  AvFormatDecoder *nd = (AvFormatDecoder *)opaque;
3058  VideoFrame *frame = (VideoFrame*)data;
3059 
3060  struct vdpau_render_state *render = (struct vdpau_render_state *)frame->buf;
3062 
3063  if (nd && nd->GetPlayer())
3064  nd->GetPlayer()->DeLimboFrame(frame);
3065 }
3066 
3067 int render_wrapper_vdpau(struct AVCodecContext *s, AVFrame *src,
3068  const VdpPictureInfo *info,
3069  uint32_t count,
3070  const VdpBitstreamBuffer *buffers)
3071 {
3072  if (!src)
3073  return -1;
3074 
3075  if (s && src && s->opaque && src->opaque)
3076  {
3077  AvFormatDecoder *nd = (AvFormatDecoder *)(s->opaque);
3078  VideoFrame *frame = (VideoFrame *)src->opaque;
3079  struct vdpau_render_state data;
3080 
3081  data.surface = (VdpVideoSurface)(uintptr_t)src->data[3];
3082  data.bitstream_buffers_used = count;
3083  data.bitstream_buffers = (VdpBitstreamBuffer*)buffers;
3084 
3085  // Store information we will require in DrawSlice()
3086  frame->priv[0] = (unsigned char*)&data;
3087  frame->priv[1] = (unsigned char*)info;
3088 
3089  nd->GetPlayer()->DrawSlice(frame, 0, 0, 0, 0);
3090  }
3091  else
3092  {
3093  LOG(VB_GENERAL, LOG_ERR, LOC +
3094  "render_wrapper_vdpau called with bad avctx or src");
3095  }
3096 
3097  return 0;
3098 }
3099 #endif // USING_VDPAU
3100 
3101 #ifdef USING_DXVA2
3102 int get_avf_buffer_dxva2(struct AVCodecContext *c, AVFrame *pic, int /*flags*/)
3103 {
3104  AvFormatDecoder *nd = (AvFormatDecoder *)(c->opaque);
3105  VideoFrame *frame = nd->GetPlayer()->GetNextVideoFrame();
3106 
3107  for (int i = 0; i < 4; i++)
3108  {
3109  pic->data[i] = nullptr;
3110  pic->linesize[i] = 0;
3111  }
3112  pic->opaque = frame;
3113  frame->pix_fmt = c->pix_fmt;
3114  pic->reordered_opaque = c->reordered_opaque;
3115  pic->data[0] = (uint8_t*)frame->buf;
3116  pic->data[3] = (uint8_t*)frame->buf;
3117 
3118  // Set release method
3119  AVBufferRef *buffer =
3120  av_buffer_create((uint8_t*)frame, 0, release_avf_buffer, nd, 0);
3121  pic->buf[0] = buffer;
3122 
3123  return 0;
3124 }
3125 #endif
3126 
3127 #ifdef USING_VAAPI
3128 int get_avf_buffer_vaapi(struct AVCodecContext *c, AVFrame *pic, int /*flags*/)
3129 {
3130  AvFormatDecoder *nd = (AvFormatDecoder *)(c->opaque);
3131  VideoFrame *frame = nd->GetPlayer()->GetNextVideoFrame();
3132 
3133  for (int i = 0; i < 4; i++)
3134  {
3135  pic->data[i] = nullptr;
3136  pic->linesize[i] = 0;
3137  }
3138  pic->opaque = frame;
3139  frame->pix_fmt = c->pix_fmt;
3140 
3141  if (nd->GetPlayer())
3142  {
3143  nd->GetPlayer()->GetDecoderContext(frame->buf, pic->data[3]);
3144  }
3145 
3146  // Set release method
3147  AVBufferRef *buffer =
3148  av_buffer_create((uint8_t*)frame, 0, release_avf_buffer, nd, 0);
3149  pic->buf[0] = buffer;
3150 
3151  return 0;
3152 }
3153 #endif
3154 
3155 #ifdef USING_VAAPI2
3156 int get_avf_buffer_vaapi2(struct AVCodecContext *c, AVFrame *pic, int flags)
3157 {
3158  AvFormatDecoder *nd = (AvFormatDecoder *)(c->opaque);
3159  nd->directrendering = false;
3160  return avcodec_default_get_buffer2(c, pic, flags);
3161 }
3162 #endif
3163 
3164 void AvFormatDecoder::DecodeDTVCC(const uint8_t *buf, uint len, bool scte)
3165 {
3166  if (!len)
3167  return;
3168 
3169  // closed caption data
3170  //cc_data() {
3171  // reserved 1 0.0 1
3172  // process_cc_data_flag 1 0.1 bslbf
3173  bool process_cc_data = buf[0] & 0x40;
3174  if (!process_cc_data)
3175  return; // early exit if process_cc_data_flag false
3176 
3177  // additional_data_flag 1 0.2 bslbf
3178  //bool additional_data = buf[0] & 0x20;
3179  // cc_count 5 0.3 uimsbf
3180  uint cc_count = buf[0] & 0x1f;
3181  // em_data 8 1.0
3182 
3183  if (len < 2+(3*cc_count))
3184  return;
3185 
3186  DecodeCCx08(buf+2, cc_count*3, scte);
3187 }
3188 
3189 void AvFormatDecoder::DecodeCCx08(const uint8_t *buf, uint len, bool scte)
3190 {
3191  if (len < 3)
3192  return;
3193 
3194  bool had_608 = false, had_708 = false;
3195  for (uint cur = 0; cur + 3 < len; cur += 3)
3196  {
3197  uint cc_code = buf[cur];
3198  bool cc_valid = cc_code & 0x04;
3199 
3200  uint data1 = buf[cur+1];
3201  uint data2 = buf[cur+2];
3202  uint data = (data2 << 8) | data1;
3203  uint cc_type = cc_code & 0x03;
3204 
3205  if (!cc_valid)
3206  {
3207  if (cc_type >= 0x2)
3209  continue;
3210  }
3211 
3212  if (scte || cc_type <= 0x1) // EIA-608 field-1/2
3213  {
3214  uint field;
3215  if (cc_type == 0x2)
3216  {
3217  // SCTE repeated field
3218  field = !last_scte_field;
3220  }
3221  else
3222  {
3223  field = cc_type ^ invert_scte_field;
3224  }
3225 
3227  {
3228  // in film mode, we may start at the wrong field;
3229  // correct if XDS start/cont/end code is detected
3230  // (must be field 2)
3231  if (scte && field == 0 &&
3232  (data1 & 0x7f) <= 0x0f && (data1 & 0x7f) != 0x00)
3233  {
3234  if (cc_type == 1)
3235  invert_scte_field = 0;
3236  field = 1;
3237 
3238  // flush decoder
3239  ccd608->FormatCC(0, -1, -1);
3240  }
3241 
3242  had_608 = true;
3243  ccd608->FormatCCField(lastccptsu / 1000, field, data);
3244 
3245  last_scte_field = field;
3246  }
3247  }
3248  else
3249  {
3250  had_708 = true;
3251  ccd708->decode_cc_data(cc_type, data1, data2);
3252  }
3253  }
3254  UpdateCaptionTracksFromStreams(had_608, had_708);
3255 }
3256 
3258  bool check_608, bool check_708)
3259 {
3260  bool need_change_608 = false;
3261  bool seen_608[4];
3262  if (check_608)
3263  {
3264  ccd608->GetServices(15/*seconds*/, seen_608);
3265  for (uint i = 0; i < 4; i++)
3266  {
3267  need_change_608 |= (seen_608[i] && !ccX08_in_tracks[i]) ||
3268  (!seen_608[i] && ccX08_in_tracks[i] && !ccX08_in_pmt[i]);
3269  }
3270  }
3271 
3272  bool need_change_708 = false;
3273  bool seen_708[64];
3274  if (check_708 || need_change_608)
3275  {
3276  ccd708->services(15/*seconds*/, seen_708);
3277  for (uint i = 1; i < 64 && !need_change_608 && !need_change_708; i++)
3278  {
3279  need_change_708 |= (seen_708[i] && !ccX08_in_tracks[i+4]) ||
3280  (!seen_708[i] && ccX08_in_tracks[i+4] && !ccX08_in_pmt[i+4]);
3281  }
3282  if (need_change_708 && !check_608)
3283  ccd608->GetServices(15/*seconds*/, seen_608);
3284  }
3285 
3286  if (!need_change_608 && !need_change_708)
3287  return;
3288 
3290 
3291  stream_tracks.clear();
3292  stream_track_types.clear();
3294  int lang = iso639_str3_to_key("und");
3295  for (uint i = 1; i < 64; i++)
3296  {
3297  if (seen_708[i] && !ccX08_in_pmt[i+4])
3298  {
3299  StreamInfo si(av_index, lang, 0/*lang_idx*/,
3300  i, false/*easy*/, true/*wide*/);
3301  stream_tracks.push_back(si);
3303  }
3304  }
3305  for (uint i = 0; i < 4; i++)
3306  {
3307  if (seen_608[i] && !ccX08_in_pmt[i])
3308  {
3309  if (0==i)
3311  else if (2==i)
3313  else
3314  lang = iso639_str3_to_key("und");
3315 
3316  StreamInfo si(av_index, lang, 0/*lang_idx*/,
3317  i+1, false/*easy*/, false/*wide*/);
3318  stream_tracks.push_back(si);
3320  }
3321  }
3323 }
3324 
3326  AVPacket *pkt, bool can_reliably_parse_keyframes)
3327 {
3328  if (prevgoppos != 0 && keyframedist != 1)
3329  {
3330  int tempKeyFrameDist = framesRead - 1 - prevgoppos;
3331  bool reset_kfd = false;
3332 
3333  if (!gopset || livetv) // gopset: we've seen 2 keyframes
3334  {
3335  LOG(VB_PLAYBACK, LOG_INFO, LOC +
3336  "gopset not set, syncing positionMap");
3337  SyncPositionMap();
3338  if (tempKeyFrameDist > 0 && !livetv)
3339  {
3340  LOG(VB_PLAYBACK, LOG_INFO, LOC +
3341  QString("Initial key frame distance: %1.")
3342  .arg(keyframedist));
3343  gopset = true;
3344  reset_kfd = true;
3345  }
3346  }
3347  else if (keyframedist != tempKeyFrameDist && tempKeyFrameDist > 0)
3348  {
3349  LOG(VB_PLAYBACK, LOG_INFO, LOC +
3350  QString("Key frame distance changed from %1 to %2.")
3351  .arg(keyframedist).arg(tempKeyFrameDist));
3352  reset_kfd = true;
3353  }
3354 
3355  if (reset_kfd)
3356  {
3357  keyframedist = tempKeyFrameDist;
3359 
3361 
3362 #if 0
3363  // also reset length
3364  QMutexLocker locker(&m_positionMapLock);
3365  if (!m_positionMap.empty())
3366  {
3367  long long index = m_positionMap.back().index;
3368  long long totframes = index * keyframedist;
3369  uint length = (uint)((totframes * 1.0f) / fps);
3370  m_parent->SetFileLength(length, totframes);
3371  }
3372 #endif
3373  }
3374  }
3375 
3376  lastKey = prevgoppos = framesRead - 1;
3377 
3378  if (can_reliably_parse_keyframes &&
3380  {
3381  long long last_frame = 0;
3382  {
3383  QMutexLocker locker(&m_positionMapLock);
3384  if (!m_positionMap.empty())
3385  last_frame = m_positionMap.back().index;
3386  }
3387 
3388 #if 0
3389  LOG(VB_PLAYBACK, LOG_DEBUG, LOC +
3390  QString("framesRead: %1 last_frame: %2 keyframedist: %3")
3391  .arg(framesRead) .arg(last_frame) .arg(keyframedist));
3392 #endif
3393 
3394  // if we don't have an entry, fill it in with what we've just parsed
3395  if (framesRead > last_frame && keyframedist > 0)
3396  {
3397  long long startpos = pkt->pos;
3398 
3399  LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_INFO, LOC +
3400  QString("positionMap[ %1 ] == %2.")
3401  .arg(framesRead).arg(startpos));
3402 
3403  PosMapEntry entry = {framesRead, framesRead, startpos};
3404 
3405  QMutexLocker locker(&m_positionMapLock);
3406  // Create a dummy positionmap entry for frame 0 so that
3407  // seeking will work properly. (See
3408  // DecoderBase::FindPosition() which subtracts
3409  // DecoderBase::indexOffset from each frame number.)
3410  if (m_positionMap.empty())
3411  {
3412  PosMapEntry dur = {0, 0, 0};
3413  m_positionMap.push_back(dur);
3414  }
3415  m_positionMap.push_back(entry);
3416  if (trackTotalDuration)
3417  {
3419  llround(totalDuration.num * 1000.0 / totalDuration.den);
3421  }
3422  }
3423 
3424 #if 0
3425  // If we are > 150 frames in and saw no positionmap at all, reset
3426  // length based on the actual bitrate seen so far
3427  if (framesRead > 150 && !recordingHasPositionMap && !livetv)
3428  {
3429  bitrate = (int)((pkt->pos * 8 * fps) / (framesRead - 1));
3430  float bytespersec = (float)bitrate / 8;
3431  float secs = ringBuffer->GetRealFileSize() * 1.0 / bytespersec;
3432  m_parent->SetFileLength((int)(secs), (int)(secs * fps));
3433  }
3434 #endif
3435  }
3436 }
3437 
3438 #define SEQ_START 0x000001b3
3439 #define GOP_START 0x000001b8
3440 #define PICTURE_START 0x00000100
3441 #define SLICE_MIN 0x00000101
3442 #define SLICE_MAX 0x000001af
3443 #define SEQ_END_CODE 0x000001b7
3444 
3445 void AvFormatDecoder::MpegPreProcessPkt(AVStream *stream, AVPacket *pkt)
3446 {
3447  AVCodecContext *context = gCodecMap->getCodecContext(stream);
3448  const uint8_t *bufptr = pkt->data;
3449  const uint8_t *bufend = pkt->data + pkt->size;
3450 
3451  while (bufptr < bufend)
3452  {
3453  bufptr = avpriv_find_start_code(bufptr, bufend, &start_code_state);
3454 
3455  float aspect_override = -1.0f;
3456  if (ringBuffer->IsDVD())
3457  aspect_override = ringBuffer->DVD()->GetAspectOverride();
3458 
3460  continue;
3461  else if (SEQ_START == start_code_state)
3462  {
3463  if (bufptr + 11 >= pkt->data + pkt->size)
3464  continue; // not enough valid data...
3465  SequenceHeader *seq = reinterpret_cast<SequenceHeader*>(
3466  const_cast<uint8_t*>(bufptr));
3467 
3468  uint width = seq->width() >> context->lowres;
3469  uint height = seq->height() >> context->lowres;
3470  current_aspect = seq->aspect(context->codec_id ==
3471  AV_CODEC_ID_MPEG1VIDEO);
3472  if (stream->sample_aspect_ratio.num)
3473  current_aspect = av_q2d(stream->sample_aspect_ratio) *
3474  width / height;
3475  if (aspect_override > 0.0f)
3476  current_aspect = aspect_override;
3477  float seqFPS = seq->fps();
3478 
3479  bool changed =
3480  (seqFPS > static_cast<float>(fps)+0.01f) ||
3481  (seqFPS < static_cast<float>(fps)-0.01f);
3482  changed |= (width != (uint)current_width );
3483  changed |= (height != (uint)current_height);
3484 
3485  if (changed)
3486  {
3487  if (private_dec)
3488  private_dec->Reset();
3489 
3490  m_parent->SetVideoParams(width, height, seqFPS, kScan_Detect);
3491 
3492  current_width = width;
3493  current_height = height;
3494  fps = seqFPS;
3495 
3496  gopset = false;
3497  prevgoppos = 0;
3499  firstvptsinuse = true;
3500  faulty_pts = faulty_dts = 0;
3503  pts_detected = false;
3504  reordered_pts_detected = false;
3505 
3506  // fps debugging info
3507  float avFPS = normalized_fps(stream, context);
3508  if ((seqFPS > avFPS+0.01f) || (seqFPS < avFPS-0.01f))
3509  {
3510  LOG(VB_PLAYBACK, LOG_INFO, LOC +
3511  QString("avFPS(%1) != seqFPS(%2)")
3512  .arg(avFPS).arg(seqFPS));
3513  }
3514  }
3515 
3516  seq_count++;
3517 
3518  if (!seen_gop && seq_count > 1)
3519  {
3520  HandleGopStart(pkt, true);
3521  pkt->flags |= AV_PKT_FLAG_KEY;
3522  }
3523  }
3524  else if (GOP_START == start_code_state)
3525  {
3526  HandleGopStart(pkt, true);
3527  seen_gop = true;
3528  pkt->flags |= AV_PKT_FLAG_KEY;
3529  }
3530  }
3531 }
3532 
3533 // Returns the number of frame starts identified in the packet.
3534 int AvFormatDecoder::H264PreProcessPkt(AVStream *stream, AVPacket *pkt)
3535 {
3536  AVCodecContext *context = gCodecMap->getCodecContext(stream);
3537  const uint8_t *buf = pkt->data;
3538  const uint8_t *buf_end = pkt->data + pkt->size;
3539  int num_frames = 0;
3540 
3541  // crude NAL unit vs Annex B detection.
3542  // the parser only understands Annex B
3543  if (context->extradata && context->extradata_size >= 4)
3544  {
3545  int nal_size = 0;
3546  int size_length = (context->extradata[4] & 0x3) + 1;
3547 
3548  for (int i = 0; i < size_length; i++)
3549  nal_size += buf[i];
3550 
3551  if (nal_size)
3552  {
3553  if (pkt->flags & AV_PKT_FLAG_KEY)
3554  HandleGopStart(pkt, false);
3555  return 1;
3556  }
3557  }
3558 
3559  while (buf < buf_end)
3560  {
3561  buf += m_h264_parser->addBytes(buf, buf_end - buf, 0);
3562 
3563  if (m_h264_parser->stateChanged())
3564  {
3566  {
3567  if (m_h264_parser->onFrameStart())
3568  ++num_frames;
3569 
3571  continue;
3572  }
3573  else
3574  {
3575  continue;
3576  }
3577  }
3578  else
3579  {
3580  continue;
3581  }
3582 
3586  float seqFPS = m_h264_parser->frameRate();
3587 
3588  bool res_changed = ((width != (uint)current_width) ||
3589  (height != (uint)current_height));
3590  bool fps_changed =
3591  (seqFPS > 0.0f) &&
3592  ((seqFPS > static_cast<float>(fps) + 0.01f) ||
3593  (seqFPS < static_cast<float>(fps) - 0.01f));
3594 
3595  if (fps_changed || res_changed)
3596  {
3597  if (private_dec)
3598  private_dec->Reset();
3599 
3600  m_parent->SetVideoParams(width, height, seqFPS, kScan_Detect);
3601 
3602  current_width = width;
3603  current_height = height;
3604 
3605  if (seqFPS > 0.0f)
3606  fps = seqFPS;
3607 
3608  gopset = false;
3609  prevgoppos = 0;
3611  firstvptsinuse = true;
3612  faulty_pts = faulty_dts = 0;
3615  pts_detected = false;
3616  reordered_pts_detected = false;
3617 
3618  // fps debugging info
3619  float avFPS = normalized_fps(stream, context);
3620  if ((seqFPS > avFPS+0.01f) || (seqFPS < avFPS-0.01f))
3621  {
3622  LOG(VB_PLAYBACK, LOG_INFO, LOC +
3623  QString("avFPS(%1) != seqFPS(%2)")
3624  .arg(avFPS).arg(seqFPS));
3625  }
3626 
3627  }
3628 
3629  HandleGopStart(pkt, true);
3630  pkt->flags |= AV_PKT_FLAG_KEY;
3631  }
3632 
3633  return num_frames;
3634 }
3635 
3636 bool AvFormatDecoder::PreProcessVideoPacket(AVStream *curstream, AVPacket *pkt)
3637 {
3638  AVCodecContext *context = gCodecMap->getCodecContext(curstream);
3639  int num_frames = 1;
3640 
3641  if (CODEC_IS_FFMPEG_MPEG(context->codec_id))
3642  {
3643  MpegPreProcessPkt(curstream, pkt);
3644  }
3645  else if (CODEC_IS_H264(context->codec_id))
3646  {
3647  num_frames = H264PreProcessPkt(curstream, pkt);
3648  }
3649  else
3650  {
3651  if (pkt->flags & AV_PKT_FLAG_KEY)
3652  {
3653  HandleGopStart(pkt, false);
3654  seen_gop = true;
3655  }
3656  else
3657  {
3658  seq_count++;
3659  if (!seen_gop && seq_count > 1)
3660  {
3661  HandleGopStart(pkt, false);
3662  }
3663  }
3664  }
3665 
3666  if (framesRead == 0 && !justAfterChange &&
3667  !(pkt->flags & AV_PKT_FLAG_KEY))
3668  {
3669  av_packet_unref(pkt);
3670  return false;
3671  }
3672 
3673  framesRead += num_frames;
3674 
3675  if (trackTotalDuration)
3676  {
3677  // The ffmpeg libraries represent a frame interval of a
3678  // 59.94fps video as 1501/90000 seconds, when it should
3679  // actually be 1501.5/90000 seconds.
3680  AVRational pkt_dur = AVRationalInit(pkt->duration);
3681  pkt_dur = av_mul_q(pkt_dur, curstream->time_base);
3682  if (pkt_dur.num == 1501 && pkt_dur.den == 90000)
3683  pkt_dur = AVRationalInit(1001, 60000); // 1501.5/90000
3684  totalDuration = av_add_q(totalDuration, pkt_dur);
3685  }
3686 
3687  justAfterChange = false;
3688 
3689  if (exitafterdecoded)
3690  gotVideoFrame = true;
3691 
3692  return true;
3693 }
3694 
3695 // Maximum retries - 500 = 5 seconds
3696 #define PACKET_MAX_RETRIES 5000
3697 #define RETRY_WAIT_TIME 10000 // microseconds
3698 bool AvFormatDecoder::ProcessVideoPacket(AVStream *curstream, AVPacket *pkt)
3699 {
3700  int retryCount = 0;
3701  int ret = 0, gotpicture = 0;
3702  AVCodecContext *context = gCodecMap->getCodecContext(curstream);
3703  MythAVFrame mpa_pic;
3704  if (!mpa_pic)
3705  {
3706  return false;
3707  }
3708  mpa_pic->reordered_opaque = AV_NOPTS_VALUE;
3709 
3710  if (pkt->pts != AV_NOPTS_VALUE)
3711  pts_detected = true;
3712 
3713  bool tryAgain = true;
3714  bool sentPacket = false;
3715  int ret2 = 0;
3716  while (tryAgain)
3717  {
3718  tryAgain = false;
3719  gotpicture = 0;
3720  avcodeclock->lock();
3721  if (private_dec)
3722  {
3723  if (QString(ic->iformat->name).contains("avi") || !pts_detected)
3724  pkt->pts = pkt->dts;
3725  // TODO disallow private decoders for dvd playback
3726  // N.B. we do not reparse the frame as it breaks playback for
3727  // everything but libmpeg2
3728  ret = private_dec->GetFrame(curstream, mpa_pic, &gotpicture, pkt);
3729  sentPacket = true;
3730  }
3731  else
3732  {
3733  if (!use_frame_timing)
3734  context->reordered_opaque = pkt->pts;
3735 
3736  // SUGGESTION
3737  // Now that avcodec_decode_video2 is deprecated and replaced
3738  // by 2 calls (receive frame and send packet), this could be optimized
3739  // into separate routines or separate threads.
3740  // Also now that it always consumes a whole buffer some code
3741  // in the caller may be able to be optimized.
3742 
3743  // FilteredReceiveFrame will call avcodec_receive_frame and
3744  // apply any codec-dependent filtering
3745  ret = m_mythcodecctx->FilteredReceiveFrame(context, mpa_pic);
3746 
3747  if (ret == 0)
3748  gotpicture = 1;
3749  else
3750  gotpicture = 0;
3751  if (ret == AVERROR(EAGAIN))
3752  ret = 0;
3753  // If we got a picture do not send the packet until we have
3754  // all available pictures
3755  if (ret==0 && !gotpicture)
3756  {
3757  ret2 = avcodec_send_packet(context, pkt);
3758  if (ret2 == AVERROR(EAGAIN))
3759  {
3760  tryAgain = true;
3761  ret2 = 0;
3762  }
3763  else
3764  {
3765  sentPacket = true;
3766  }
3767  }
3768  }
3769  avcodeclock->unlock();
3770 
3771  if (ret < 0 || ret2 < 0)
3772  {
3773  char error[AV_ERROR_MAX_STRING_SIZE];
3774  if (ret < 0)
3775  {
3776  LOG(VB_GENERAL, LOG_ERR, LOC +
3777  QString("video avcodec_receive_frame error: %1 (%2) gotpicture:%3")
3778  .arg(av_make_error_string(error, sizeof(error), ret))
3779  .arg(ret).arg(gotpicture));
3780  }
3781  if (ret2 < 0)
3782  LOG(VB_GENERAL, LOG_ERR, LOC +
3783  QString("video avcodec_send_packet error: %1 (%2) gotpicture:%3")
3784  .arg(av_make_error_string(error, sizeof(error), ret2))
3785  .arg(ret2).arg(gotpicture));
3787  {
3788  // If erroring on GPU assist, try switching to software decode
3790  m_parent->SetErrored(QObject::tr("Video Decode Error"));
3791  else
3792  m_streams_changed = true;
3793  }
3794  if (ret == AVERROR_EXTERNAL || ret2 == AVERROR_EXTERNAL)
3795  m_streams_changed = true;
3796  return false;
3797  }
3798 
3799  if (tryAgain)
3800  {
3801  if (++retryCount > PACKET_MAX_RETRIES)
3802  {
3803  LOG(VB_GENERAL, LOG_ERR, LOC +
3804  QString("ERROR: Video decode buffering retries exceeded maximum"));
3805  return false;
3806  }
3807  LOG(VB_PLAYBACK, LOG_INFO, LOC +
3808  QString("Video decode buffering retry"));
3809  usleep(RETRY_WAIT_TIME);
3810  }
3811  }
3812  // averror_count counts sequential errors, so if you have a successful
3813  // packet then reset it
3814  averror_count = 0;
3815  if (gotpicture)
3816  {
3817  LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_INFO, LOC +
3818  QString("video timecodes packet-pts:%1 frame-pts:%2 packet-dts: %3 frame-dts:%4")
3819  .arg(pkt->pts).arg(mpa_pic->pts).arg(pkt->pts)
3820  .arg(mpa_pic->pkt_dts));
3821 
3822  if (!use_frame_timing)
3823  {
3824  int64_t pts = 0;
3825 
3826  // Detect faulty video timestamps using logic from ffplay.
3827  if (pkt->dts != AV_NOPTS_VALUE)
3828  {
3829  faulty_dts += (pkt->dts <= last_dts_for_fault_detection);
3830  last_dts_for_fault_detection = pkt->dts;
3831  }
3832  if (mpa_pic->reordered_opaque != AV_NOPTS_VALUE)
3833  {
3834  faulty_pts += (mpa_pic->reordered_opaque <= last_pts_for_fault_detection);
3835  last_pts_for_fault_detection = mpa_pic->reordered_opaque;
3836  reordered_pts_detected = true;
3837  }
3838 
3839  // Explicity use DTS for DVD since they should always be valid for every
3840  // frame and fixups aren't enabled for DVD.
3841  // Select reordered_opaque (PTS) timestamps if they are less faulty or the
3842  // the DTS timestamp is missing. Also use fixups for missing PTS instead of
3843  // DTS to avoid oscillating between PTS and DTS. Only select DTS if PTS is
3844  // more faulty or never detected.
3846  {
3847  if (pkt->dts != AV_NOPTS_VALUE)
3848  pts = pkt->dts;
3849  pts_selected = false;
3850  }
3851  else if (ringBuffer->IsDVD())
3852  {
3853  if (pkt->dts != AV_NOPTS_VALUE)
3854  pts = pkt->dts;
3855  pts_selected = false;
3856  }
3857  else if (private_dec && private_dec->NeedsReorderedPTS() &&
3858  mpa_pic->reordered_opaque != AV_NOPTS_VALUE)
3859  {
3860  pts = mpa_pic->reordered_opaque;
3861  pts_selected = true;
3862  }
3864  {
3865  if (mpa_pic->reordered_opaque != AV_NOPTS_VALUE)
3866  pts = mpa_pic->reordered_opaque;
3867  pts_selected = true;
3868  }
3869  else if (pkt->dts != AV_NOPTS_VALUE)
3870  {
3871  pts = pkt->dts;
3872  pts_selected = false;
3873  }
3874 
3875  LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_DEBUG, LOC +
3876  QString("video packet timestamps reordered %1 pts %2 dts %3 (%4)")
3877  .arg(mpa_pic->reordered_opaque).arg(pkt->pts).arg(pkt->dts)
3878  .arg((force_dts_timestamps) ? "dts forced" :
3879  (pts_selected) ? "reordered" : "dts"));
3880 
3881  mpa_pic->reordered_opaque = pts;
3882  }
3883  ProcessVideoFrame(curstream, mpa_pic);
3884  }
3885  if (!sentPacket)
3886  {
3887  // MythTV logic expects that only one frame is processed
3888  // Save the packet for later and return.
3889  AVPacket *newPkt = new AVPacket;
3890  memset(newPkt, 0, sizeof(AVPacket));
3891  av_init_packet(newPkt);
3892  av_packet_ref(newPkt, pkt);
3893  storedPackets.prepend(newPkt);
3894  }
3895  return true;
3896 }
3897 
3898 bool AvFormatDecoder::ProcessVideoFrame(AVStream *stream, AVFrame *mpa_pic)
3899 {
3900 
3901  AVCodecContext *context = gCodecMap->getCodecContext(stream);
3902 
3903  // We need to mediate between ATSC and SCTE data when both are present. If
3904  // both are present, we generally want to prefer ATSC. However, there may
3905  // be large sections of the recording where ATSC is used and other sections
3906  // where SCTE is used. In that case, we want to allow a natural transition
3907  // from ATSC back to SCTE. We do this by allowing 10 consecutive SCTE
3908  // frames, without an intervening ATSC frame, to cause a switch back to
3909  // considering SCTE frames. The number 10 is somewhat arbitrarily chosen.
3910 
3911  uint cc_len = (uint) max(mpa_pic->scte_cc_len,0);
3912  uint8_t *cc_buf = mpa_pic->scte_cc_buf;
3913  bool scte = true;
3914 
3915  // If we saw SCTE, then decrement a nonzero ignore_scte count.
3916  if (cc_len > 0 && ignore_scte)
3917  --ignore_scte;
3918 
3919  // If both ATSC and SCTE caption data are available, prefer ATSC
3920  if ((mpa_pic->atsc_cc_len > 0) || ignore_scte)
3921  {
3922  cc_len = (uint) max(mpa_pic->atsc_cc_len, 0);
3923  cc_buf = mpa_pic->atsc_cc_buf;
3924  scte = false;
3925  // If we explicitly saw ATSC, then reset ignore_scte count.
3926  if (cc_len > 0)
3927  ignore_scte = 10;
3928  }
3929 
3930  // Decode CEA-608 and CEA-708 captions
3931  for (uint i = 0; i < cc_len; i += ((cc_buf[i] & 0x1f) * 3) + 2)
3932  DecodeDTVCC(cc_buf + i, cc_len - i, scte);
3933 
3934  if (cc_len == 0) {
3935  // look for A53 captions
3936  AVFrameSideData *side_data = av_frame_get_side_data(mpa_pic, AV_FRAME_DATA_A53_CC);
3937  if (side_data && (side_data->size > 0)) {
3938  DecodeCCx08(side_data->data, side_data->size, false);
3939  }
3940  }
3941 
3942  VideoFrame *picframe = (VideoFrame *)(mpa_pic->opaque);
3943 
3945  {
3946  // Do nothing, we just want the pts, captions, subtites, etc.
3947  // So we can release the unconverted blank video frame to the
3948  // display queue.
3949  }
3950  else if (!directrendering)
3951  {
3952  AVFrame *tmp_frame = nullptr;
3953  AVFrame *use_frame = nullptr;
3954  VideoFrame *xf = picframe;
3955  picframe = m_parent->GetNextVideoFrame();
3956  unsigned char *buf = picframe->buf;
3957  bool used_picframe=false;
3958 #ifdef USING_VAAPI2
3959  if (IS_VAAPI_PIX_FMT((AVPixelFormat)mpa_pic->format))
3960  {
3961  int ret = 0;
3962  tmp_frame = av_frame_alloc();
3963  use_frame = tmp_frame;
3964  /* retrieve data from GPU to CPU */
3965  AVPixelFormat *pixelformats = nullptr;
3966  ret = av_hwframe_transfer_get_formats(mpa_pic->hw_frames_ctx,
3967  AV_HWFRAME_TRANSFER_DIRECTION_FROM,
3968  &pixelformats, 0);
3969  if (ret==0)
3970  {
3971  for (AVPixelFormat *format = pixelformats; *format != AV_PIX_FMT_NONE; format++)
3972  {
3973  if (*format == AV_PIX_FMT_YUV420P)
3974  {
3975  // Retrieve the picture directly into the Video Frame Buffer
3976  used_picframe = true;
3977  use_frame->format = AV_PIX_FMT_YUV420P;
3978  for (int i = 0; i < 3; i++)
3979  {
3980  use_frame->data[i] = buf + picframe->offsets[i];
3981  use_frame->linesize[i] = picframe->pitches[i];
3982  }
3983  // Dummy release method - we do not want to free the buffer
3984  AVBufferRef *buffer =
3985  av_buffer_create((uint8_t*)picframe, 0, dummy_release_avf_buffer, this, 0);
3986  use_frame->buf[0] = buffer;
3987  use_frame->width = mpa_pic->width;
3988  use_frame->height = mpa_pic->height;
3989  break;
3990  }
3991  }
3992  }
3993  if ((ret = av_hwframe_transfer_data(use_frame, mpa_pic, 0)) < 0)
3994  {
3995  LOG(VB_GENERAL, LOG_ERR, LOC
3996  + QString("Error %1 transferring the data to system memory")
3997  .arg(ret));
3998  av_frame_free(&use_frame);
3999  return false;
4000  }
4001  av_freep(&pixelformats);
4002  }
4003  else
4004 #endif // USING_VAAPI2
4005  use_frame = mpa_pic;
4006 
4007  if (!used_picframe)
4008  {
4009  AVFrame tmppicture;
4010  av_image_fill_arrays(tmppicture.data, tmppicture.linesize,
4011  buf, AV_PIX_FMT_YUV420P, use_frame->width,
4012  use_frame->height, IMAGE_ALIGN);
4013  tmppicture.data[0] = buf + picframe->offsets[0];
4014  tmppicture.data[1] = buf + picframe->offsets[1];
4015  tmppicture.data[2] = buf + picframe->offsets[2];
4016  tmppicture.linesize[0] = picframe->pitches[0];
4017  tmppicture.linesize[1] = picframe->pitches[1];
4018  tmppicture.linesize[2] = picframe->pitches[2];
4019 
4020  QSize dim = get_video_dim(*context);
4021  sws_ctx = sws_getCachedContext(sws_ctx, use_frame->width,
4022  use_frame->height, (AVPixelFormat)use_frame->format,
4023  use_frame->width, use_frame->height,
4024  AV_PIX_FMT_YUV420P, SWS_FAST_BILINEAR,
4025  nullptr, nullptr, nullptr);
4026  if (!sws_ctx)
4027  {
4028  LOG(VB_GENERAL, LOG_ERR, LOC + "Failed to allocate sws context");
4029  return false;
4030  }
4031  sws_scale(sws_ctx, use_frame->data, use_frame->linesize, 0, dim.height(),
4032  tmppicture.data, tmppicture.linesize);
4033  }
4034  if (xf)
4035  {
4036  // Set the frame flags, but then discard it
4037  // since we are not using it for display.
4038  xf->interlaced_frame = mpa_pic->interlaced_frame;
4039  xf->top_field_first = mpa_pic->top_field_first;
4040  xf->frameNumber = framesPlayed;
4041  xf->aspect = current_aspect;
4043  }
4044  if (tmp_frame)
4045  av_frame_free(&tmp_frame);
4046  }
4047  else if (!picframe)
4048  {
4049  LOG(VB_GENERAL, LOG_ERR, LOC + "NULL videoframe - direct rendering not"
4050  "correctly initialized.");
4051  return false;
4052  }
4053 
4054  long long pts;
4055  if (use_frame_timing)
4056  {
4057  pts = mpa_pic->pts;
4058  if (pts == AV_NOPTS_VALUE)
4059  pts = mpa_pic->pkt_dts;
4060  if (pts == AV_NOPTS_VALUE)
4061  pts = mpa_pic->reordered_opaque;
4062  if (pts == AV_NOPTS_VALUE)
4063  {
4064  LOG(VB_GENERAL, LOG_ERR, LOC + "No PTS found - unable to process video.");
4065  return false;
4066  }
4067  pts = (long long)(av_q2d(stream->time_base) *
4068  pts * 1000);
4069  }
4070  else
4071  pts = (long long)(av_q2d(stream->time_base) *
4072  mpa_pic->reordered_opaque * 1000);
4073 
4074  long long temppts = pts;
4075  // Validate the video pts against the last pts. If it's
4076  // a little bit smaller, equal or missing, compute
4077  // it from the last. Otherwise assume a wraparound.
4078  if (!ringBuffer->IsDVD() &&
4079  temppts <= lastvpts &&
4080  (temppts + (1000 / fps) > lastvpts || temppts <= 0))
4081  {
4082  temppts = lastvpts;
4083  temppts += (long long)(1000 / fps);
4084  // MPEG2/H264 frames can be repeated, update pts accordingly
4085  temppts += (long long)(mpa_pic->repeat_pict * 500 / fps);
4086  }
4087 
4088  // Calculate actual fps from the pts values.
4089  long long ptsdiff = temppts - lastvpts;
4090  double calcfps = 1000.0 / ptsdiff;
4091  if (calcfps < 121.0 && calcfps > 3.0)
4092  {
4093  // If fps has doubled due to frame-doubling deinterlace
4094  // Set fps to double value.
4095  double fpschange = calcfps / fps;
4096  int prior = fpsMultiplier;
4097  if (fpschange > 1.9 && fpschange < 2.1)
4098  fpsMultiplier = 2;
4099  if (fpschange > 0.9 && fpschange < 1.1)
4100  fpsMultiplier = 1;
4101  if (fpsMultiplier != prior)
4103  }
4104 
4105  LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_INFO, LOC +
4106  QString("video timecode %1 %2 %3 %4%5")
4107  .arg(use_frame_timing ? mpa_pic->pts : mpa_pic->reordered_opaque).arg(pts)
4108  .arg(temppts).arg(lastvpts)
4109  .arg((pts != temppts) ? " fixup" : ""));
4110 
4111  if (picframe)
4112  {
4113  picframe->interlaced_frame = mpa_pic->interlaced_frame;
4114  picframe->top_field_first = mpa_pic->top_field_first;
4115  picframe->repeat_pict = mpa_pic->repeat_pict;
4116  picframe->disp_timecode = NormalizeVideoTimecode(stream, temppts);
4117  picframe->frameNumber = framesPlayed;
4118  picframe->aspect = current_aspect;
4119  picframe->dummy = 0;
4120  picframe->directrendering = directrendering ? 1 : 0;
4121 
4122  m_parent->ReleaseNextVideoFrame(picframe, temppts);
4123  }
4124 
4125  decoded_video_frame = picframe;
4126  gotVideoFrame = true;
4127  if (++fpsSkip >= fpsMultiplier)
4128  {
4129  ++framesPlayed;
4130  fpsSkip = 0;
4131  }
4132 
4133  lastvpts = temppts;
4134  if (!firstvpts && firstvptsinuse)
4135  firstvpts = temppts;
4136 
4137  return true;
4138 }
4139 
4146  const AVStream *stream, const AVPacket *pkt)
4147 {
4148  (void) stream;
4149 
4150  const uint8_t *buf = pkt->data;
4151  uint64_t linemask = 0;
4152  unsigned long long utc = lastccptsu;
4153 
4154  // [i]tv0 means there is a linemask
4155  // [I]TV0 means there is no linemask and all lines are present
4156  if ((buf[0]=='t') && (buf[1]=='v') && (buf[2] == '0'))
4157  {
4159  memcpy(&linemask, buf + 3, 8);
4160  buf += 11;
4161  }
4162  else if ((buf[0]=='T') && (buf[1]=='V') && (buf[2] == '0'))
4163  {
4164  linemask = 0xffffffffffffffffLL;
4165  buf += 3;
4166  }
4167  else
4168  {
4169  LOG(VB_VBI, LOG_ERR, LOC + QString("Unknown VBI data stream '%1%2%3'")
4170  .arg(QChar(buf[0])).arg(QChar(buf[1])).arg(QChar(buf[2])));
4171  return;
4172  }
4173 
4174  static const uint min_blank = 6;
4175  for (uint i = 0; i < 36; i++)
4176  {
4177  if (!((linemask >> i) & 0x1))
4178  continue;
4179 
4180  const uint line = ((i < 18) ? i : i-18) + min_blank;
4181  const uint field = (i<18) ? 0 : 1;
4182  const uint id2 = *buf & 0xf;
4183  switch (id2)
4184  {
4185  case VBI_TYPE_TELETEXT:
4186  // SECAM lines 6-23
4187  // PAL lines 6-22
4188  // NTSC lines 10-21 (rare)
4189  if (tracks[kTrackTypeTeletextMenu].empty())
4190  {
4191  StreamInfo si(pkt->stream_index, 0, 0, 0, 0);
4192  tracks[kTrackTypeTeletextMenu].push_back(si);
4193  }
4194  ttd->Decode(buf+1, VBI_IVTV);
4195  break;
4196  case VBI_TYPE_CC:
4197  // PAL line 22 (rare)
4198  // NTSC line 21
4199  if (21 == line)
4200  {
4201  int data = (buf[2] << 8) | buf[1];
4203  ccd608->FormatCCField(utc/1000, field, data);
4204  utc += 33367;
4205  }
4206  break;
4207  case VBI_TYPE_VPS: // Video Programming System
4208  // PAL line 16
4209  ccd608->DecodeVPS(buf+1); // a.k.a. PDC
4210  break;
4211  case VBI_TYPE_WSS: // Wide Screen Signal
4212  // PAL line 23
4213  // NTSC line 20
4214  ccd608->DecodeWSS(buf+1);
4215  break;
4216  }
4217  buf += 43;
4218  }
4219  lastccptsu = utc;
4220  UpdateCaptionTracksFromStreams(true, false);
4221 }
4222 
4228  const AVStream*, const AVPacket *pkt)
4229 {
4230  const uint8_t *buf = pkt->data;
4231  const uint8_t *buf_end = pkt->data + pkt->size;
4232 
4233 
4234  while (buf < buf_end)
4235  {
4236  if (*buf == 0x10)
4237  {
4238  buf++; // skip
4239  }
4240  else if (*buf == 0x02)
4241  {
4242  buf += 4;
4243  if ((buf_end - buf) >= 42)
4244  ttd->Decode(buf, VBI_DVB);
4245  buf += 42;
4246  }
4247  else if (*buf == 0x03)
4248  {
4249  buf += 4;
4250  if ((buf_end - buf) >= 42)
4251  ttd->Decode(buf, VBI_DVB_SUBTITLE);
4252  buf += 42;
4253  }
4254  else if (*buf == 0xff)
4255  {
4256  buf += 3;
4257  }
4258  else
4259  {
4260  LOG(VB_VBI, LOG_ERR, LOC +
4261  QString("VBI: Unknown descriptor: %1").arg(*buf));
4262  buf += 46;
4263  }
4264  }
4265 }
4266 
4271  const AVStream *str, const AVPacket *pkt)
4272 {
4273 #ifdef USING_MHEG
4274  if (!itv && ! (itv = m_parent->GetInteractiveTV()))
4275  return;
4276 
4277  // The packet may contain several tables.
4278  uint8_t *data = pkt->data;
4279  int length = pkt->size;
4280  int componentTag, dataBroadcastId;
4281  unsigned carouselId;
4282  {
4283  QMutexLocker locker(avcodeclock);
4284  componentTag = str->component_tag;
4285  dataBroadcastId = str->data_id;
4286  carouselId = (unsigned) str->carousel_id;
4287  }
4288  while (length > 3)
4289  {
4290  uint16_t sectionLen = (((data[1] & 0xF) << 8) | data[2]) + 3;
4291 
4292  if (sectionLen > length) // This may well be filler
4293  return;
4294 
4295  itv->ProcessDSMCCSection(data, sectionLen,
4296  componentTag, carouselId,
4297  dataBroadcastId);
4298  length -= sectionLen;
4299  data += sectionLen;
4300  }
4301 #else
4302  Q_UNUSED(str);
4303  Q_UNUSED(pkt);
4304 #endif // USING_MHEG
4305 }
4306 
4307 bool AvFormatDecoder::ProcessSubtitlePacket(AVStream *curstream, AVPacket *pkt)
4308 {
4309  if (!m_parent->GetSubReader(pkt->stream_index))
4310  return true;
4311 
4312  long long pts = 0;
4313 
4314  if (pkt->dts != AV_NOPTS_VALUE)
4315  pts = (long long)(av_q2d(curstream->time_base) * pkt->dts * 1000);
4316 
4317  avcodeclock->lock();
4319  bool isForcedTrack = selectedTrack[kTrackTypeSubtitle].forced;
4320  avcodeclock->unlock();
4321 
4322  int gotSubtitles = 0;
4323  AVSubtitle subtitle;
4324  memset(&subtitle, 0, sizeof(AVSubtitle));
4325 
4326  if (ringBuffer->IsDVD())
4327  {
4328  if (ringBuffer->DVD()->NumMenuButtons() > 0)
4329  {
4330  ringBuffer->DVD()->GetMenuSPUPkt(pkt->data, pkt->size,
4331  curstream->id, pts);
4332  }
4333  else
4334  {
4335  if (pkt->stream_index == subIdx)
4336  {
4337  QMutexLocker locker(avcodeclock);
4338  ringBuffer->DVD()->DecodeSubtitles(&subtitle, &gotSubtitles,
4339  pkt->data, pkt->size, pts);
4340  }
4341  }
4342  }
4343  else if (decodeAllSubtitles || pkt->stream_index == subIdx)
4344  {
4345  AVCodecContext *ctx = gCodecMap->getCodecContext(curstream);
4346  QMutexLocker locker(avcodeclock);
4347  avcodec_decode_subtitle2(ctx, &subtitle, &gotSubtitles,
4348  pkt);
4349 
4350  subtitle.start_display_time += pts;
4351  subtitle.end_display_time += pts;
4352  }
4353 
4354  if (gotSubtitles)
4355  {
4356  if (isForcedTrack)
4357  subtitle.forced = true;
4358  LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_INFO, LOC +
4359  QString("subtl timecode %1 %2 %3 %4")
4360  .arg(pkt->pts).arg(pkt->dts)
4361  .arg(subtitle.start_display_time)
4362  .arg(subtitle.end_display_time));
4363 
4364  bool forcedon = m_parent->GetSubReader(pkt->stream_index)->AddAVSubtitle(
4365  subtitle, curstream->codecpar->codec_id == AV_CODEC_ID_XSUB,
4367  m_parent->EnableForcedSubtitles(forcedon || isForcedTrack);
4368  }
4369 
4370  return true;
4371 }
4372 
4374 {
4375  if (!(decodeAllSubtitles ||
4376  selectedTrack[kTrackTypeRawText].av_stream_index == pkt->stream_index))
4377  {
4378  return false;
4379  }
4380 
4381  if (!m_parent->GetSubReader(pkt->stream_index+0x2000))
4382  return false;
4383 
4384  QTextCodec *codec = QTextCodec::codecForName("utf-8");
4385  QTextDecoder *dec = codec->makeDecoder();
4386  QString text = dec->toUnicode((const char*)pkt->data, pkt->size - 1);
4387  QStringList list = text.split('\n', QString::SkipEmptyParts);
4388  delete dec;
4389 
4390  m_parent->GetSubReader(pkt->stream_index+0x2000)->
4391  AddRawTextSubtitle(list, pkt->duration);
4392 
4393  return true;
4394 }
4395 
4396 bool AvFormatDecoder::ProcessDataPacket(AVStream *curstream, AVPacket *pkt,
4397  DecodeType decodetype)
4398 {
4399  enum AVCodecID codec_id = curstream->codecpar->codec_id;
4400 
4401  switch (codec_id)
4402  {
4403  case AV_CODEC_ID_MPEG2VBI:
4404  ProcessVBIDataPacket(curstream, pkt);
4405  break;
4406  case AV_CODEC_ID_DVB_VBI:
4407  ProcessDVBDataPacket(curstream, pkt);
4408  break;
4409  case AV_CODEC_ID_DSMCC_B:
4410  {
4411  ProcessDSMCCPacket(curstream, pkt);
4413  // Have to return regularly to ensure that the OSD is updated.
4414  // This applies both to MHEG and also channel browsing.
4415 #ifdef USING_MHEG
4416  if (!(decodetype & kDecodeVideo))
4417  allowedquit |= (itv && itv->ImageHasChanged());
4418 #else
4419  Q_UNUSED(decodetype);
4420 #endif // USING_MHEG:
4421  break;
4422  }
4423  default:
4424  break;
4425  }
4426  return true;
4427 }
4428 
4430 {
4431  bool ret = DecoderBase::SetTrack(type, trackNo);
4432 
4433  if (kTrackTypeAudio == type)
4434  {
4435  QString msg = SetupAudioStream() ? "" : "not ";
4436  LOG(VB_AUDIO, LOG_INFO, LOC + "Audio stream type "+msg+"changed.");
4437  }
4438 
4439  return ret;
4440 }
4441 
4443 {
4444  if (!ic || trackNo >= tracks[type].size())
4445  return "";
4446 
4447  bool forced = tracks[type][trackNo].forced;
4448  int lang_key = tracks[type][trackNo].language;
4449  QString forcedString = forced ? QObject::tr(" (forced)") : "";
4450  if (kTrackTypeAudio == type)
4451  {
4452  QString msg = iso639_key_toName(lang_key);
4453 
4454  switch (tracks[type][trackNo].audio_type)
4455  {
4456  case kAudioTypeNormal :
4457  {
4458  int av_index = tracks[kTrackTypeAudio][trackNo].av_stream_index;
4459  AVStream *s = ic->streams[av_index];
4460 
4461  if (s)
4462  {
4463  AVCodecParameters *par = s->codecpar;
4464  AVCodecContext *ctx = gCodecMap->getCodecContext(s);
4465  if (par->codec_id == AV_CODEC_ID_MP3)
4466  msg += QString(" MP3");
4467  else if (ctx && ctx->codec)
4468  msg += QString(" %1").arg(ctx->codec->name).toUpper();
4469 
4470  int channels = 0;
4471  if (ringBuffer->IsDVD() || par->channels)
4472  channels = tracks[kTrackTypeAudio][trackNo].orig_num_channels;
4473 
4474  if (channels == 0)
4475  msg += QString(" ?ch");
4476  else if((channels > 4) && !(channels & 1))
4477  msg += QString(" %1.1ch").arg(channels - 1);
4478  else
4479  msg += QString(" %1ch").arg(channels);
4480  }
4481 
4482  break;
4483  }
4485  case kAudioTypeCommentary :
4487  case kAudioTypeCleanEffects :
4488  case kAudioTypeSpokenSubs :
4489  default :
4490  msg += QString(" (%1)")
4491  .arg(toString(tracks[type][trackNo].audio_type));
4492  break;
4493  }
4494 
4495  return QString("%1: %2").arg(trackNo + 1).arg(msg);
4496  }
4497  else if (kTrackTypeSubtitle == type)
4498  {
4499  return QObject::tr("Subtitle") + QString(" %1: %2%3")
4500  .arg(trackNo + 1).arg(iso639_key_toName(lang_key))
4501  .arg(forcedString);
4502  }
4503  else if (forced && kTrackTypeRawText == type)
4504  {
4505  return DecoderBase::GetTrackDesc(type, trackNo) + forcedString;
4506  }
4507  else
4508  {
4509  return DecoderBase::GetTrackDesc(type, trackNo);
4510  }
4511 }
4512 
4514 {
4515  return ttd->GetDecoderType();
4516 }
4517 
4518 QString AvFormatDecoder::GetXDS(const QString &key) const
4519 {
4520  return ccd608->GetXDS(key);
4521 }
4522 
4523 QByteArray AvFormatDecoder::GetSubHeader(uint trackNo) const
4524 {
4525  if (trackNo >= tracks[kTrackTypeSubtitle].size())
4526  return QByteArray();
4527 
4528  int index = tracks[kTrackTypeSubtitle][trackNo].av_stream_index;
4529  AVCodecContext *ctx = gCodecMap->getCodecContext(ic->streams[index]);
4530  if (!ctx)
4531  return QByteArray();
4532 
4533  return QByteArray((char *)ctx->subtitle_header,
4534  ctx->subtitle_header_size);
4535 }
4536 
4537 void AvFormatDecoder::GetAttachmentData(uint trackNo, QByteArray &filename,
4538  QByteArray &data)
4539 {
4540  if (trackNo >= tracks[kTrackTypeAttachment].size())
4541  return;
4542 
4543  int index = tracks[kTrackTypeAttachment][trackNo].av_stream_index;
4544  AVDictionaryEntry *tag = av_dict_get(ic->streams[index]->metadata,
4545  "filename", nullptr, 0);
4546  if (tag)
4547  filename = QByteArray(tag->value);
4548  AVCodecParameters *par = ic->streams[index]->codecpar;
4549  data = QByteArray((char *)par->extradata, par->extradata_size);
4550 }
4551 
4553 {
4554  for (uint i = 0; i < tracks[kTrackTypeAudio].size(); i++)
4555  {
4556  AVStream *s = ic->streams[tracks[kTrackTypeAudio][i].av_stream_index];
4557  if (s)
4558  {
4559  if ((s->component_tag == tag) ||
4560  ((tag <= 0) && s->component_tag <= 0))
4561  {
4562  return SetTrack(kTrackTypeAudio, i);
4563  }
4564  }
4565  }
4566  return false;
4567 }
4568 
4570 {
4571  for (uint i = 0; i < ic->nb_streams; i++)
4572  {
4573  AVStream *s = ic->streams[i];
4574  if (s)
4575  {
4576  if (s->component_tag == tag)
4577  {
4578  StreamInfo si(i, 0, 0, 0, 0);
4580  return true;
4581  }
4582  }
4583  }
4584  return false;
4585 }
4586 
4587 // documented in decoderbase.cpp
4589 {
4590  if (kTrackTypeAudio == type)
4591  return AutoSelectAudioTrack();
4592 
4594  return -1;
4595 
4597 }
4598 
4599 static vector<int> filter_lang(const sinfo_vec_t &tracks, int lang_key,
4600  const vector<int> &ftype)
4601 {
4602  vector<int> ret;
4603 
4604  vector<int>::const_iterator it = ftype.begin();
4605  for (; it != ftype.end(); ++it)
4606  {
4607  if ((lang_key < 0) || tracks[*it].language == lang_key)
4608  ret.push_back(*it);
4609  }
4610 
4611  return ret;
4612 }
4613 
4614 static vector<int> filter_type(const sinfo_vec_t &tracks, AudioTrackType type)
4615 {
4616  vector<int> ret;
4617 
4618  for (uint i = 0; i < tracks.size(); i++)
4619  {
4620  if (tracks[i].audio_type == type)
4621  ret.push_back(i);
4622  }
4623 
4624  return ret;
4625 }
4626 
4627 int AvFormatDecoder::filter_max_ch(const AVFormatContext *ic,
4628  const sinfo_vec_t &tracks,
4629  const vector<int> &fs,
4630  enum AVCodecID codecId,
4631  int profile)
4632 {
4633  int selectedTrack = -1, max_seen = -1;
4634 
4635  vector<int>::const_iterator it = fs.begin();
4636  for (; it != fs.end(); ++it)
4637  {
4638  const int stream_index = tracks[*it].av_stream_index;
4639  AVCodecParameters *par = ic->streams[stream_index]->codecpar;
4640  if ((codecId == AV_CODEC_ID_NONE || codecId == par->codec_id) &&
4641  (max_seen < par->channels))
4642  {
4643  if (codecId == AV_CODEC_ID_DTS && profile > 0)
4644  {
4645  // we cannot decode dts-hd, so only select it if passthrough
4646  if (!DoPassThrough(par, true) || par->profile != profile)
4647  continue;
4648  }
4649  selectedTrack = *it;
4650  max_seen = par->channels;
4651  }
4652  }
4653 
4654  return selectedTrack;
4655 }
4656 
4704 {
4705  const sinfo_vec_t &atracks = tracks[kTrackTypeAudio];
4708  int &ctrack = currentTrack[kTrackTypeAudio];
4709 
4710  uint numStreams = atracks.size();
4711  if ((ctrack >= 0) && (ctrack < (int)numStreams))
4712  return ctrack; // audio already selected
4713 
4714 #if 0
4715  // enable this to print streams
4716  for (uint i = 0; i < atracks.size(); i++)
4717  {
4718  int idx = atracks[i].av_stream_index;
4719  AVCodecContext *codec_ctx = ic->streams[idx]->codec;
4720  AudioInfo item(codec_ctx->codec_id, codec_ctx->bps,
4721  codec_ctx->sample_rate, codec_ctx->channels,
4722  DoPassThrough(codec_ctx, true));
4723  LOG(VB_AUDIO, LOG_DEBUG, LOC + " * " + item.toString());
4724  }
4725 #endif
4726 
4727  int selTrack = (1 == numStreams) ? 0 : -1;
4728  int wlang = wtrack.language;
4729 
4730 
4731  if ((selTrack < 0) && (wtrack.av_substream_index >= 0))
4732  {
4733  LOG(VB_AUDIO, LOG_INFO, LOC + "Trying to reselect audio sub-stream");
4734  // Dual stream without language information: choose
4735  // the previous substream that was kept in wtrack,
4736  // ignoring the stream index (which might have changed).
4737  int substream_index = wtrack.av_substream_index;
4738 
4739  for (uint i = 0; i < numStreams; i++)
4740  {
4741  if (atracks[i].av_substream_index == substream_index)
4742  {
4743  selTrack = i;
4744  break;
4745  }
4746  }
4747  }
4748 
4749  if ((selTrack < 0) && wlang >= -1 && numStreams)
4750  {
4751  LOG(VB_AUDIO, LOG_INFO, LOC + "Trying to reselect audio track");
4752  // Try to reselect user selected audio stream.
4753  // This should find the stream after a commercial
4754  // break and in some cases after a channel change.
4755  uint windx = wtrack.language_index;
4756  for (uint i = 0; i < numStreams; i++)
4757  {
4758  if (wlang == atracks[i].language)
4759  {
4760  selTrack = i;
4761 
4762  if (windx == atracks[i].language_index)
4763  break;
4764  }
4765  }
4766  }
4767 
4768  if (selTrack < 0 && numStreams)
4769  {
4770  LOG(VB_AUDIO, LOG_INFO, LOC + "Trying to select audio track (w/lang)");
4771 
4772  // Filter out commentary and audio description tracks
4773  vector<int> ftype = filter_type(atracks, kAudioTypeNormal);
4774 
4775  if (ftype.empty())
4776  {
4777  LOG(VB_AUDIO, LOG_WARNING, "No audio tracks matched the type filter, "
4778  "so trying all tracks.");
4779  for (int i = 0; i < static_cast<int>(atracks.size()); i++)
4780  ftype.push_back(i);
4781  }
4782 
4783  // try to get the language track matching the frontend language.
4784  QString language_key_convert = iso639_str2_to_str3(gCoreContext->GetLanguage());
4785  uint language_key = iso639_str3_to_key(language_key_convert);
4786  uint canonical_key = iso639_key_to_canonical_key(language_key);
4787 
4788  vector<int> flang = filter_lang(atracks, canonical_key, ftype);
4789 
4790  if (m_audio->CanDTSHD())
4791  selTrack = filter_max_ch(ic, atracks, flang, AV_CODEC_ID_DTS,
4792  FF_PROFILE_DTS_HD_MA);
4793  if (selTrack < 0)
4794  selTrack = filter_max_ch(ic, atracks, flang, AV_CODEC_ID_TRUEHD);
4795 
4796  if (selTrack < 0 && m_audio->CanDTSHD())
4797  selTrack = filter_max_ch(ic, atracks, flang, AV_CODEC_ID_DTS,
4798  FF_PROFILE_DTS_HD_HRA);
4799  if (selTrack < 0)
4800  selTrack = filter_max_ch(ic, atracks, flang, AV_CODEC_ID_EAC3);
4801 
4802  if (selTrack < 0)
4803  selTrack = filter_max_ch(ic, atracks, flang, AV_CODEC_ID_DTS);
4804 
4805  if (selTrack < 0)
4806  selTrack = filter_max_ch(ic, atracks, flang, AV_CODEC_ID_AC3);
4807 
4808  if (selTrack < 0)
4809  selTrack = filter_max_ch(ic, atracks, flang);
4810 
4811  // try to get best track for most preferred language
4812  // Set by the "Guide Data" language prefs in Appearance.
4813  if (selTrack < 0)
4814  {
4815  vector<int>::const_iterator it = languagePreference.begin();
4816  for (; it != languagePreference.end() && selTrack < 0; ++it)
4817  {
4818  flang = filter_lang(atracks, *it, ftype);
4819 
4820  if (m_audio->CanDTSHD())
4821  selTrack = filter_max_ch(ic, atracks, flang, AV_CODEC_ID_DTS,
4822  FF_PROFILE_DTS_HD_MA);
4823  if (selTrack < 0)
4824  selTrack = filter_max_ch(ic, atracks, flang,
4825  AV_CODEC_ID_TRUEHD);
4826 
4827  if (selTrack < 0 && m_audio->CanDTSHD())
4828  selTrack = filter_max_ch(ic, atracks, flang, AV_CODEC_ID_DTS,
4829  FF_PROFILE_DTS_HD_HRA);
4830 
4831  if (selTrack < 0)
4832  selTrack = filter_max_ch(ic, atracks, flang,
4833  AV_CODEC_ID_EAC3);
4834 
4835  if (selTrack < 0)
4836  selTrack = filter_max_ch(ic, atracks, flang, AV_CODEC_ID_DTS);
4837 
4838  if (selTrack < 0)
4839  selTrack = filter_max_ch(ic, atracks, flang, AV_CODEC_ID_AC3);
4840 
4841  if (selTrack < 0)
4842  selTrack = filter_max_ch(ic, atracks, flang);
4843  }
4844  }
4845 
4846  // could not select track based on user preferences (language)
4847  // try to select the default track
4848  if (selTrack < 0 && numStreams)
4849  {
4850  LOG(VB_AUDIO, LOG_INFO, LOC + "Trying to select default track");
4851  for (uint i = 0; i < atracks.size(); i++) {
4852  int idx = atracks[i].av_stream_index;
4853  if (ic->streams[idx]->disposition & AV_DISPOSITION_DEFAULT)
4854  {
4855  selTrack = i;
4856  break;
4857  }
4858  }
4859  }
4860 
4861  // try to get best track for any language
4862  if (selTrack < 0)
4863  {
4864  LOG(VB_AUDIO, LOG_INFO, LOC +
4865  "Trying to select audio track (wo/lang)");
4866  flang = filter_lang(atracks, -1, ftype);
4867 
4868  if (m_audio->CanDTSHD())
4869  selTrack = filter_max_ch(ic, atracks, flang, AV_CODEC_ID_DTS,
4870  FF_PROFILE_DTS_HD_MA);
4871  if (selTrack < 0)
4872  selTrack = filter_max_ch(ic, atracks, flang, AV_CODEC_ID_TRUEHD);
4873 
4874  if (selTrack < 0 && m_audio->CanDTSHD())
4875  selTrack = filter_max_ch(ic, atracks, flang, AV_CODEC_ID_DTS,
4876  FF_PROFILE_DTS_HD_HRA);
4877 
4878  if (selTrack < 0)
4879  selTrack = filter_max_ch(ic, atracks, flang, AV_CODEC_ID_EAC3);
4880 
4881  if (selTrack < 0)
4882  selTrack = filter_max_ch(ic, atracks, flang, AV_CODEC_ID_DTS);
4883 
4884  if (selTrack < 0)
4885  selTrack = filter_max_ch(ic, atracks, flang, AV_CODEC_ID_AC3);
4886 
4887  if (selTrack < 0)
4888  selTrack = filter_max_ch(ic, atracks, flang);
4889  }
4890  }
4891 
4892  if (selTrack < 0)
4893  {
4894  strack.av_stream_index = -1;
4895  if (ctrack != selTrack)
4896  {
4897  LOG(VB_AUDIO, LOG_INFO, LOC + "No suitable audio track exists.");
4898  ctrack = selTrack;
4899  }
4900  }
4901  else
4902  {
4903  ctrack = selTrack;
4904  strack = atracks[selTrack];
4905 
4906  if (wtrack.av_stream_index < 0)
4907  wtrack = strack;
4908 
4909  LOG(VB_AUDIO, LOG_INFO, LOC +
4910  QString("Selected track %1 (A/V Stream #%2)")
4911  .arg(GetTrackDesc(kTrackTypeAudio, ctrack))
4912  .arg(strack.av_stream_index));
4913  }
4914 
4915  SetupAudioStream();
4916  return selTrack;
4917 }
4918 
4919 static void extract_mono_channel(uint channel, AudioInfo *audioInfo,
4920  char *buffer, int bufsize)
4921 {
4922  // Only stereo -> mono (left or right) is supported
4923  if (audioInfo->channels != 2)
4924  return;
4925 
4926  if (channel >= (uint)audioInfo->channels)
4927  return;
4928 
4929  const uint samplesize = audioInfo->sample_size;
4930  const uint samples = bufsize / samplesize;
4931  const uint halfsample = samplesize >> 1;
4932 
4933  const char *from = (channel == 1) ? buffer + halfsample : buffer;
4934  char *to = (channel == 0) ? buffer + halfsample : buffer;
4935 
4936  for (uint sample = 0; sample < samples;
4937  (sample++), (from += samplesize), (to += samplesize))
4938  {
4939  memmove(to, from, halfsample);
4940  }
4941 }
4942 
4943 bool AvFormatDecoder::ProcessAudioPacket(AVStream *curstream, AVPacket *pkt,
4944  DecodeType decodetype)
4945 {
4946  AVCodecContext *ctx = gCodecMap->getCodecContext(curstream);
4947  int ret = 0;
4948  int data_size = 0;
4949  bool firstloop = true;
4950  int decoded_size = -1;
4951 
4952  avcodeclock->lock();
4955  avcodeclock->unlock();
4956 
4957  AVPacket tmp_pkt;
4958  av_init_packet(&tmp_pkt);
4959  tmp_pkt.data = pkt->data;
4960  tmp_pkt.size = pkt->size;
4961 
4962  while (tmp_pkt.size > 0)
4963  {
4964  bool reselectAudioTrack = false;
4965 
4967  if (!m_audio->HasAudioIn())
4968  {
4969  LOG(VB_AUDIO, LOG_INFO, LOC +
4970  "Audio is disabled - trying to restart it");
4971  reselectAudioTrack = true;
4972  }
4974 
4975  // detect switches between stereo and dual languages
4976  bool wasDual = audSubIdx != -1;
4977  bool isDual = ctx->avcodec_dual_language;
4978  if ((wasDual && !isDual) || (!wasDual && isDual))
4979  {
4981  reselectAudioTrack = true;
4982  }
4983 
4984  // detect channels on streams that need
4985  // to be decoded before we can know this
4986  bool already_decoded = false;
4987  if (!ctx->channels)
4988  {
4989  QMutexLocker locker(avcodeclock);
4990 
4991  if (DoPassThrough(curstream->codecpar, false) || !DecoderWillDownmix(ctx))
4992  {
4993  // for passthru or codecs for which the decoder won't downmix
4994  // let the decoder set the number of channels. For other codecs
4995  // we downmix if necessary in audiooutputbase
4996  ctx->request_channel_layout = 0;
4997  }
4998  else // No passthru, the decoder will downmix
4999  {
5000  ctx->request_channel_layout =
5001  av_get_default_channel_layout(m_audio->GetMaxChannels());
5002  if (ctx->codec_id == AV_CODEC_ID_AC3)
5003  ctx->channels = m_audio->GetMaxChannels();
5004  }
5005 
5006  ret = m_audio->DecodeAudio(ctx, audioSamples, data_size, &tmp_pkt);
5007  decoded_size = data_size;
5008  already_decoded = true;
5009  reselectAudioTrack |= ctx->channels;
5010  }
5011 
5012  if (reselectAudioTrack)
5013  {
5014  QMutexLocker locker(avcodeclock);
5017  audIdx = -1;
5018  audSubIdx = -1;
5022  }
5023 
5024  if (!(decodetype & kDecodeAudio) || (pkt->stream_index != audIdx)
5025  || !m_audio->HasAudioOut())
5026  break;
5027 
5028  if (firstloop && pkt->pts != AV_NOPTS_VALUE)
5029  lastapts = (long long)(av_q2d(curstream->time_base) * pkt->pts * 1000);
5030 
5031  if (!use_frame_timing)
5032  {
5033  // This code under certain conditions causes jump backwards to lose
5034  // audio.
5035  if (skipaudio && selectedTrack[kTrackTypeVideo].av_stream_index > -1)
5036  {
5037  if ((lastapts < lastvpts - (10.0 / fps)) || lastvpts == 0)
5038  break;
5039  else
5040  skipaudio = false;
5041  }
5042 
5043  // skip any audio frames preceding first video frame
5045  {
5046  LOG(VB_PLAYBACK | VB_TIMESTAMP, LOG_INFO, LOC +
5047  QString("discarding early audio timecode %1 %2 %3")
5048  .arg(pkt->pts).arg(pkt->dts).arg(lastapts));
5049  break;
5050  }
5051  }
5052  firstvptsinuse = false;
5053 
5054  avcodeclock->lock();
5055  data_size = 0;
5056 
5057  // Check if the number of channels or sampling rate have changed
5058  if (ctx->sample_rate != audioOut.sample_rate ||
5059  ctx->channels != audioOut.channels ||
5061  ctx->bits_per_raw_sample) != audioOut.format)
5062  {
5063  LOG(VB_GENERAL, LOG_INFO, LOC + "Audio stream changed");
5064  if (ctx->channels != audioOut.channels)
5065  {
5066  LOG(VB_GENERAL, LOG_INFO, LOC + QString("Number of audio channels changed from %1 to %2")
5067  .arg(audioOut.channels).arg(ctx->channels));
5068  }
5071  audIdx = -1;
5073  }
5074 
5075  if (audioOut.do_passthru)
5076  {
5077  if (!already_decoded)
5078  {
5080  {
5081  ret = m_audio->DecodeAudio(ctx, audioSamples, data_size, &tmp_pkt);
5082  decoded_size = data_size;
5083  }
5084  else
5085  {
5086  decoded_size = -1;
5087  }
5088  }
5089  memcpy(audioSamples, tmp_pkt.data, tmp_pkt.size);
5090  data_size = tmp_pkt.size;
5091  // We have processed all the data, there can't be any left
5092  tmp_pkt.size = 0;
5093  }
5094  else
5095  {
5096  if (!already_decoded)
5097  {
5098  if (DecoderWillDownmix(ctx))
5099  {
5100  ctx->request_channel_layout =
5101  av_get_default_channel_layout(m_audio->GetMaxChannels());
5102  }
5103  else
5104  {
5105  ctx->request_channel_layout = 0;
5106  }
5107 
5108  ret = m_audio->DecodeAudio(ctx, audioSamples, data_size, &tmp_pkt);
5109  decoded_size = data_size;
5110  }
5111  }
5112  avcodeclock->unlock();
5113 
5114  if (ret < 0)
5115  {
5116  LOG(VB_GENERAL, LOG_ERR, LOC + "Unknown audio decoding error");
5117  return false;
5118  }
5119 
5120  if (data_size <= 0)
5121  {
5122  tmp_pkt.data += ret;
5123  tmp_pkt.size -= ret;
5124  continue;
5125  }
5126 
5127  long long temppts = lastapts;
5128 
5129  if (audSubIdx != -1 && !audioOut.do_passthru)
5130  extract_mono_channel(audSubIdx, &audioOut,
5131  (char *)audioSamples, data_size);
5132 
5133  int samplesize = AudioOutputSettings::SampleSize(m_audio->GetFormat());
5134  int frames = (ctx->channels <= 0 || decoded_size < 0 || !samplesize) ? -1 :
5135  decoded_size / (ctx->channels * samplesize);
5136  m_audio->AddAudioData((char *)audioSamples, data_size, temppts, frames);
5138  {
5140  }
5141  else
5142  {
5143  lastapts += (long long)
5144  ((double)(frames * 1000) / ctx->sample_rate);
5145  }
5146 
5147  LOG(VB_TIMESTAMP, LOG_INFO, LOC + QString("audio timecode %1 %2 %3 %4")
5148  .arg(pkt->pts).arg(pkt->dts).arg(temppts).arg(lastapts));
5149 
5152 
5153  tmp_pkt.data += ret;
5154  tmp_pkt.size -= ret;
5155  firstloop = false;
5156  }
5157 
5158  return true;
5159 }
5160 
5161 // documented in decoderbase.h
5163 {
5164  AVPacket *pkt = nullptr;
5165  bool have_err = false;
5166 
5167  const DecodeType origDecodetype = decodetype;
5168 
5169  gotVideoFrame = false;
5170 
5171  frame_decoded = 0;
5172  decoded_video_frame = nullptr;
5173 
5174  allowedquit = false;
5175  bool storevideoframes = false;
5176 
5177  avcodeclock->lock();
5178  AutoSelectTracks();
5179  avcodeclock->unlock();
5180 
5181  skipaudio = (lastvpts == 0);
5182 
5183  if( !m_processFrames )
5184  {
5185  return false;
5186  }
5187 
5188  hasVideo = HasVideo(ic);
5189  needDummyVideoFrames = false;
5190 
5191  if (!hasVideo && (decodetype & kDecodeVideo))
5192  {
5193  // NB This could be an issue if the video stream is not
5194  // detected initially as the video buffers will be filled.
5195  needDummyVideoFrames = true;
5196  decodetype = (DecodeType)((int)decodetype & ~kDecodeVideo);
5197  skipaudio = false;
5198  }
5199 
5201 
5204  {
5205  int got_picture = 0;
5206  AVStream *stream = ic->streams[selectedTrack[kTrackTypeVideo]
5207  .av_stream_index];
5208  MythAVFrame mpa_pic;
5209  if (!mpa_pic)
5210  {
5211  return false;
5212  }
5213 
5214  private_dec->GetFrame(stream, mpa_pic, &got_picture, nullptr);
5215  if (got_picture)
5216  ProcessVideoFrame(stream, mpa_pic);
5217  }
5218 
5219  while (!allowedquit)
5220  {
5221  if (decodetype & kDecodeAudio)
5222  {
5223  if (((currentTrack[kTrackTypeAudio] < 0) ||
5224  (selectedTrack[kTrackTypeAudio].av_stream_index < 0)))
5225  {
5226  // disable audio request if there are no audio streams anymore
5227  // and we have video, otherwise allow decoding to stop
5228  if (hasVideo)
5229  decodetype = (DecodeType)((int)decodetype & ~kDecodeAudio);
5230  else
5231  allowedquit = true;
5232  }
5233  }
5234  else
5235  if ((origDecodetype & kDecodeAudio) &&
5236  (currentTrack[kTrackTypeAudio] >= 0) &&
5237  (selectedTrack[kTrackTypeAudio].av_stream_index >= 0))
5238  {
5239  // Turn on audio decoding again if it was on originally
5240  // and an audio stream has now appeared. This can happen
5241  // in still DVD menus with audio
5242  decodetype = (DecodeType)((int)decodetype | kDecodeAudio);
5243  }
5244 
5246 
5247  if (gotVideoFrame)
5248  {
5249  if (decodetype == kDecodeNothing)
5250  {
5251  // no need to buffer audio or video if we
5252  // only care about building a keyframe map.
5253  // NB but allow for data only (MHEG) streams
5254  allowedquit = true;
5255  }
5256  else if ((decodetype & kDecodeAV) == kDecodeAV &&
5257  (storedPackets.count() < max_video_queue_size) &&
5258  // buffer audio to prevent audio buffer
5259  // underruns in case you are setting negative values
5260  // in Adjust Audio Sync.
5263  {
5264  storevideoframes = true;
5265  }
5266  else if (decodetype & kDecodeVideo)
5267  {
5268  if (storedPackets.count() >= max_video_queue_size)
5269  LOG(VB_GENERAL, LOG_WARNING, LOC +
5270  QString("Audio %1 ms behind video but already %2 "
5271  "video frames queued. AV-Sync might be broken.")
5272  .arg(lastvpts-lastapts).arg(storedPackets.count()));
5273  allowedquit = true;
5274  continue;
5275  }
5276  }
5277 
5278  if (!storevideoframes && storedPackets.count() > 0)
5279  {
5280  if (pkt)
5281  {
5282  av_packet_unref(pkt);
5283  delete pkt;
5284  }
5285  pkt = storedPackets.takeFirst();
5286  }
5287  else
5288  {
5289  if (!pkt)
5290  {
5291  pkt = new AVPacket;
5292  memset(pkt, 0, sizeof(AVPacket));
5293  av_init_packet(pkt);
5294  }
5295 
5296  int retval = 0;
5297  if (!ic || ((retval = ReadPacket(ic, pkt, storevideoframes)) < 0))
5298  {
5299  if (retval == -EAGAIN)
5300  continue;
5301 
5302  SetEof(true);
5303  delete pkt;
5304  char errbuf[256];
5305  QString errmsg;
5306  if (av_strerror(retval, errbuf, sizeof errbuf) == 0)
5307  errmsg = QString(errbuf);
5308  else
5309  errmsg = "UNKNOWN";
5310 
5311  LOG(VB_GENERAL, LOG_ERR, QString("decoding error %1 (%2)")
5312  .arg(errmsg).arg(retval));
5313  return false;
5314  }
5315 
5316  if (waitingForChange && pkt->pos >= readAdjust)
5317  FileChanged();
5318 
5319  if (pkt->pos > readAdjust)
5320  pkt->pos -= readAdjust;
5321  }
5322 
5323  if (!ic)
5324  {
5325  LOG(VB_GENERAL, LOG_ERR, LOC + "No context");
5326  av_packet_unref(pkt);
5327  continue;
5328  }
5329 
5330  if (pkt->stream_index >= (int)ic->nb_streams)
5331  {
5332  LOG(VB_GENERAL, LOG_ERR, LOC + "Bad stream");
5333  av_packet_unref(pkt);
5334  continue;
5335  }
5336 
5337  AVStream *curstream = ic->streams[pkt->stream_index];
5338 
5339  if (!curstream)
5340  {
5341  LOG(VB_GENERAL, LOG_ERR, LOC + "Bad stream (NULL)");
5342  av_packet_unref(pkt);
5343  continue;
5344  }
5345 
5346  enum AVMediaType codec_type = curstream->codecpar->codec_type;
5347 
5348  if (storevideoframes && codec_type == AVMEDIA_TYPE_VIDEO)
5349  {
5350  // av_dup_packet(pkt);
5351  storedPackets.append(pkt);
5352  pkt = nullptr;
5353  continue;
5354  }
5355 
5356  if (codec_type == AVMEDIA_TYPE_VIDEO &&
5357  pkt->stream_index == selectedTrack[kTrackTypeVideo].av_stream_index)
5358  {
5359  if (!PreProcessVideoPacket(curstream, pkt))
5360  continue;
5361 
5362  // If the resolution changed in XXXPreProcessPkt, we may
5363  // have a fatal error, so check for this before continuing.
5364  if (m_parent->IsErrored())
5365  {
5366  av_packet_unref(pkt);
5367  delete pkt;
5368  return false;
5369  }
5370  }
5371 
5372  if (codec_type == AVMEDIA_TYPE_SUBTITLE &&
5373  curstream->codecpar->codec_id == AV_CODEC_ID_TEXT)
5374  {
5375  ProcessRawTextPacket(pkt);
5376  av_packet_unref(pkt);
5377  continue;
5378  }
5379 
5380  if (codec_type == AVMEDIA_TYPE_SUBTITLE &&
5381  curstream->codecpar->codec_id == AV_CODEC_ID_DVB_TELETEXT)
5382  {
5383  ProcessDVBDataPacket(curstream, pkt);
5384  av_packet_unref(pkt);
5385  continue;
5386  }
5387 
5388  if (codec_type == AVMEDIA_TYPE_DATA)
5389  {
5390  ProcessDataPacket(curstream, pkt, decodetype);
5391  av_packet_unref(pkt);
5392  continue;
5393  }
5394 
5395  AVCodecContext *ctx = gCodecMap->getCodecContext(curstream);
5396  if (!ctx)
5397  {
5398  if (codec_type != AVMEDIA_TYPE_VIDEO)
5399  {
5400  LOG(VB_PLAYBACK, LOG_ERR, LOC +
5401  QString("No codec for stream index %1, type(%2) id(%3:%4)")
5402  .arg(pkt->stream_index)
5403  .arg(ff_codec_type_string(codec_type))
5404  .arg(ff_codec_id_string(curstream->codecpar->codec_id))
5405  .arg(curstream->codecpar->codec_id));
5406  // Process Stream Change in case we have no audio
5407  if (codec_type == AVMEDIA_TYPE_AUDIO && !m_audio->HasAudioIn())
5408  m_streams_changed = true;
5409  }
5410  av_packet_unref(pkt);
5411  continue;
5412  }
5413 
5414  have_err = false;
5415 
5416  switch (codec_type)
5417  {
5418  case AVMEDIA_TYPE_AUDIO:
5419  {
5420  if (!ProcessAudioPacket(curstream, pkt, decodetype))
5421  have_err = true;
5422  else
5424  break;
5425  }
5426 
5427  case AVMEDIA_TYPE_VIDEO:
5428  {
5429  if (pkt->stream_index != selectedTrack[kTrackTypeVideo].av_stream_index)
5430  {
5431  break;
5432  }
5433 
5434  if (pkt->pts != AV_NOPTS_VALUE)
5435  {
5436  lastccptsu = (long long)
5437  (av_q2d(curstream->time_base)*pkt->pts*1000000);
5438  }
5439 
5440  if (!(decodetype & kDecodeVideo))
5441  {
5442  framesPlayed++;
5443  gotVideoFrame = true;
5444  break;
5445  }
5446 
5447  if (!ProcessVideoPacket(curstream, pkt))
5448  have_err = true;
5449  break;
5450  }
5451 
5452  case AVMEDIA_TYPE_SUBTITLE:
5453  {
5454  if (!ProcessSubtitlePacket(curstream, pkt))
5455  have_err = true;
5456  break;
5457  }
5458 
5459  default:
5460  {
5461  LOG(VB_GENERAL, LOG_ERR, LOC +
5462  QString("Decoding - id(%1) type(%2)")
5463  .arg(ff_codec_id_string(ctx->codec_id))
5464  .arg(ff_codec_type_string(ctx->codec_type)));
5465  have_err = true;
5466  break;
5467  }
5468  }
5469 
5470  if (!have_err)
5471  frame_decoded = 1;
5472 
5473  av_packet_unref(pkt);
5474  }
5475 
5476  if (pkt)
5477  delete pkt;
5478 
5479  return true;
5480 }
5481 
5483  if (m_streams_changed)
5484  {
5485  SeekReset(0, 0, true, true);
5486  QMutexLocker locker(avcodeclock);
5487  ScanStreams(false);
5488  m_streams_changed=false;
5489  }
5490 }
5491 
5492 int AvFormatDecoder::ReadPacket(AVFormatContext *ctx, AVPacket *pkt, bool &/*storePacket*/)
5493 {
5494  QMutexLocker locker(avcodeclock);
5495 
5496  return av_read_frame(ctx, pkt);
5497 }
5498 
5499 bool AvFormatDecoder::HasVideo(const AVFormatContext *ic)
5500 {
5501  if (ic && ic->cur_pmt_sect)
5502  {
5503  const ProgramMapTable pmt(PSIPTable(ic->cur_pmt_sect));
5504 
5505  for (uint i = 0; i < pmt.StreamCount(); i++)
5506  {
5507  // MythTV remaps OpenCable Video to normal video during recording
5508  // so "dvb" is the safest choice for system info type, since this
5509  // will ignore other uses of the same stream id in DVB countries.
5510  if (pmt.IsVideo(i, "dvb"))
5511  return true;
5512 
5513  // MHEG may explicitly select a private stream as video
5514  if ((i == (uint)selectedTrack[kTrackTypeVideo].av_stream_index) &&
5515  (pmt.StreamType(i) == StreamID::PrivData))
5516  {
5517  return true;
5518  }
5519  }
5520  }
5521 
5523 }
5524 
5526 {
5527  while (needDummyVideoFrames && m_parent &&
5529  {
5531  if (!frame)
5532  return false;
5533 
5536  m_parent->DeLimboFrame(frame);
5537 
5538  frame->interlaced_frame = 0; // not interlaced
5539  frame->top_field_first = 1; // top field first
5540  frame->repeat_pict = 0; // not a repeated picture
5541  frame->frameNumber = framesPlayed;
5542  frame->dummy = 1;
5543 
5544  decoded_video_frame = frame;
5545  framesPlayed++;
5546  gotVideoFrame = true;
5547  }
5548  return true;
5549 }
5550 
5552 {
5553  if (private_dec)
5554  return private_dec->GetName();
5556 }
5557 
5559 {
5561  if (stream < 0 || !ic)
5562  return QString();
5563  return ff_codec_id_string(ic->streams[stream]->codecpar->codec_id);
5564 }
5565 
5567 {
5568  return nullptr; // TODO is this still needed
5569 }
5570 
5572 {
5573  if (selectedTrack[kTrackTypeAudio].av_stream_index < 0)
5574  {
5575  disable_passthru = disable;
5576  return;
5577  }
5578 
5579  if (disable != disable_passthru)
5580  {
5581  disable_passthru = disable;
5582  QString msg = (disable) ? "Disabling" : "Allowing";
5583  LOG(VB_AUDIO, LOG_INFO, LOC + msg + " pass through");
5584 
5585  // Force pass through state to be reanalyzed
5587  }
5588 }
5589 
5591 {
5592  QMutexLocker locker(avcodeclock);
5593 
5594  SetupAudioStream();
5595 }
5596 
5597 inline bool AvFormatDecoder::DecoderWillDownmix(const AVCodecContext *ctx)
5598 {
5599  // Until ffmpeg properly implements dialnorm
5600  // use Myth internal downmixer if machines has FPU/SSE
5602  return false;
5603  if (!m_audio->CanDownmix())
5604  return true;
5605  // use ffmpeg only for dolby codecs if we have to
5606  switch (ctx->codec_id)
5607  {
5608  case AV_CODEC_ID_AC3:
5609  case AV_CODEC_ID_TRUEHD:
5610  case AV_CODEC_ID_EAC3:
5611  return true;
5612  default:
5613  return false;
5614  }
5615 }
5616 
5617 bool AvFormatDecoder::DoPassThrough(const AVCodecParameters *par, bool withProfile)
5618 {
5619  bool passthru;
5620 
5621  // if withProfile == false, we will accept any DTS stream regardless
5622  // of its profile. We do so, so we can bitstream DTS-HD as DTS core
5623  if (!withProfile && par->codec_id == AV_CODEC_ID_DTS && !m_audio->CanDTSHD())
5624  passthru = m_audio->CanPassthrough(par->sample_rate, par->channels,
5625  par->codec_id, FF_PROFILE_DTS);
5626  else
5627  passthru = m_audio->CanPassthrough(par->sample_rate, par->channels,
5628  par->codec_id, par->profile);
5629 
5630  passthru &= !disable_passthru;
5631 
5632  return passthru;
5633 }
5634 
5643 {
5644  AudioInfo info; // no_audio
5645  AVStream *curstream = nullptr;
5646  AVCodecContext *ctx = nullptr;
5647  AudioInfo old_in = audioIn;
5648  int requested_channels;
5649 
5650  if ((currentTrack[kTrackTypeAudio] >= 0) && ic &&
5651  (selectedTrack[kTrackTypeAudio].av_stream_index <=
5652  (int) ic->nb_streams) &&
5653  (curstream = ic->streams[selectedTrack[kTrackTypeAudio]
5654  .av_stream_index]) &&
5655  (ctx = gCodecMap->getCodecContext(curstream)))
5656  {
5657  AudioFormat fmt =
5659  ctx->bits_per_raw_sample);
5660 
5661  if (av_sample_fmt_is_planar(ctx->sample_fmt))
5662  {
5663  LOG(VB_AUDIO, LOG_INFO, LOC + QString("Audio data is planar"));
5664  }
5665 
5666  if (fmt == FORMAT_NONE)
5667  {
5668  int bps = av_get_bytes_per_sample(ctx->sample_fmt) << 3;
5669  if (ctx->sample_fmt == AV_SAMPLE_FMT_S32 &&
5670  ctx->bits_per_raw_sample)
5671  bps = ctx->bits_per_raw_sample;
5672  LOG(VB_GENERAL, LOG_ERR, LOC +
5673  QString("Unsupported sample format with %1 bits").arg(bps));
5674  return false;
5675  }
5676 
5677  bool using_passthru = DoPassThrough(curstream->codecpar, false);
5678 
5679  requested_channels = ctx->channels;
5680  ctx->request_channel_layout =
5681  av_get_default_channel_layout(requested_channels);
5682 
5683  if (!using_passthru &&
5684  ctx->channels > (int)m_audio->GetMaxChannels() &&
5685  DecoderWillDownmix(ctx))
5686  {
5687  requested_channels = m_audio->GetMaxChannels();
5688  ctx->request_channel_layout =
5689  av_get_default_channel_layout(requested_channels);
5690  }
5691  else
5692  {
5693  ctx->request_channel_layout = 0;
5694  }
5695 
5696  info = AudioInfo(ctx->codec_id, fmt, ctx->sample_rate,
5697  requested_channels, using_passthru, ctx->channels,
5698  ctx->codec_id == AV_CODEC_ID_DTS ? ctx->profile : 0);
5699  }
5700 
5701  if (!ctx)
5702  {
5704  LOG(VB_PLAYBACK, LOG_INFO, LOC +
5705  "No codec context. Returning false");
5706  return false;
5707  }
5708 
5709  if (info == audioIn)
5710  return false;
5711 
5712  LOG(VB_AUDIO, LOG_INFO, LOC + "Initializing audio parms from " +
5713  QString("audio track #%1").arg(currentTrack[kTrackTypeAudio]+1));
5714 
5715  audioOut = audioIn = info;
5716 
5717  LOG(VB_AUDIO, LOG_INFO, LOC + "Audio format changed " +
5718  QString("\n\t\t\tfrom %1 to %2")
5719  .arg(old_in.toString()).arg(audioOut.toString()));
5720 
5721  m_audio->SetAudioParams(audioOut.format, ctx->channels,
5722  requested_channels,
5725  m_audio->ReinitAudio();
5726  AudioOutput *audioOutput = m_audio->GetAudioOutput();
5727  if (audioOutput)
5728  audioOutput->SetSourceBitrate(ctx->bit_rate);
5729 
5730  if (LCD *lcd = LCD::Get())
5731  {
5732  LCDAudioFormatSet audio_format;
5733 
5734  switch (ctx->codec_id)
5735  {
5736  case AV_CODEC_ID_MP2:
5737  audio_format = AUDIO_MPEG2;
5738  break;
5739  case AV_CODEC_ID_MP3:
5740  audio_format = AUDIO_MP3;
5741  break;
5742  case AV_CODEC_ID_AC3:
5743  audio_format = AUDIO_AC3;
5744  break;
5745  case AV_CODEC_ID_DTS:
5746  audio_format = AUDIO_DTS;
5747  break;
5748  case AV_CODEC_ID_VORBIS:
5749  audio_format = AUDIO_OGG;
5750  break;
5751  case AV_CODEC_ID_WMAV1:
5752  audio_format = AUDIO_WMA;
5753  break;
5754  case AV_CODEC_ID_WMAV2:
5755  audio_format = AUDIO_WMA2;
5756  break;
5757  default:
5758  audio_format = AUDIO_WAV;
5759  break;
5760  }
5761 
5762  lcd->setAudioFormatLEDs(audio_format, true);
5763 
5764  if (audioOut.do_passthru)
5765  lcd->setVariousLEDs(VARIOUS_SPDIF, true);
5766  else
5767  lcd->setVariousLEDs(VARIOUS_SPDIF, false);
5768 
5769  switch (audioIn.channels)
5770  {
5771  case 0:
5772  /* nb: aac and mp3 seem to be coming up 0 here, may point to an
5773  * avformatdecoder audio channel handling bug, per janneg */
5774  case 1:
5775  case 2:
5776  /* all audio codecs have at *least* one channel, but
5777  * LR is the fewest LED we can light up */
5778  lcd->setSpeakerLEDs(SPEAKER_LR, true);
5779  break;
5780  case 3:
5781  case 4:
5782  case 5:
5783  case 6:
5784  lcd->setSpeakerLEDs(SPEAKER_51, true);
5785  break;
5786  default:
5787  lcd->setSpeakerLEDs(SPEAKER_71, true);
5788  break;
5789  }
5790 
5791  }
5792  return true;
5793 }
5794 
5796 {
5797  int64_t start_time, end_time;
5798  int64_t duration;
5799  AVStream *st = nullptr;
5800 
5801  start_time = INT64_MAX;
5802  end_time = INT64_MIN;
5803 
5804  for (uint i = 0; i < ic->nb_streams; i++)
5805  {
5806  AVStream *st1 = ic->streams[i];
5807  if (st1 && st1->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
5808  {
5809  st = st1;
5810  break;
5811  }
5812  }
5813  if (!st)
5814  return;
5815 
5816  duration = INT64_MIN;
5817  if (st->start_time != AV_NOPTS_VALUE && st->time_base.den) {
5818  int64_t start_time1= av_rescale_q(st->start_time, st->time_base, AV_TIME_BASE_Q);
5819  if (start_time1 < start_time)
5820  start_time = start_time1;
5821  if (st->duration != AV_NOPTS_VALUE) {
5822  int64_t end_time1 = start_time1 +
5823  av_rescale_q(st->duration, st->time_base, AV_TIME_BASE_Q);
5824  if (end_time1 > end_time)
5825  end_time = end_time1;
5826  }
5827  }
5828  if (st->duration != AV_NOPTS_VALUE) {
5829  int64_t duration1 = av_rescale_q(st->duration, st->time_base, AV_TIME_BASE_Q);
5830  if (duration1 > duration)
5831  duration = duration1;
5832  }
5833  if (start_time != INT64_MAX) {
5834  ic->start_time = start_time;
5835  if (end_time != INT64_MIN) {
5836  if (end_time - start_time > duration)
5837  duration = end_time - start_time;
5838  }
5839  }
5840  if (duration != INT64_MIN) {
5841  int64_t filesize;
5842  ic->duration = duration;
5843  if (ic->pb && (filesize = avio_size(ic->pb)) > 0) {
5844  /* compute the bitrate */
5845  ic->bit_rate = (double)filesize * 8.0 * AV_TIME_BASE /
5846  (double)ic->duration;
5847  }
5848  }
5849 }
5850 
5851 /* vim: set expandtab tabstop=4 shiftwidth=4: */
bool dontSyncPositionMap
Definition: decoderbase.h:337
virtual bool DoFastForward(long long desiredFrame, bool doflush=true)
Skips ahead or rewinds to desiredFrame.
bool setDeinterlacer(bool enable, QString name=QString())
void DeLimboFrame(VideoFrame *frame)
virtual bool ProcessDataPacket(AVStream *curstream, AVPacket *pkt, DecodeType decodetype)
int pitches[3]
Y, U, & V pitches.
Definition: mythframe.h:63
int GetFreeVideoFrames(void) const
Returns the number of frames available for decoding onto.
void SetAudioParams(AudioFormat format, int orig_channels, int channels, int codec, int samplerate, bool passthru, int bitrate=-1)
Set audio output parameters.
int AutoSelectTrack(uint type) override
Select best track.
#define VBI_TYPE_TELETEXT
Definition: ivtv_myth.h:273
static bool IS_VAAPI_PIX_FMT(enum AVPixelFormat fmt)
vector< int > languagePreference
language preferences for auto-selection of streams
Definition: decoderbase.h:363
virtual bool Reset(void)=0
QString GetXDS(const QString &key) const
bool ProcessSubtitlePacket(AVStream *stream, AVPacket *pkt)
#define CODEC_IS_VAAPI(codec, enc)
Definition: mythcodecid.h:159
void SetKeyframeDistance(int keyframedistance)
Definition: mythplayer.cpp:678
void SetInInit(bool state)
MythPlayer * GetPlayer()
Definition: decoderbase.h:158
int CanonicalLanguageKey(int i) const
virtual bool DoRewind(long long desiredFrame, bool doflush=true)
static MythCodecID GetBestSupportedCodec(uint width, uint height, const QString &decoder, uint stream_type, bool no_acceleration, AVPixelFormat &pix_fmt)
ISO 639-1 and ISO 639-2 support functions.
bool ProcessAudioPacket(AVStream *stream, AVPacket *pkt, DecodeType decodetype)
void SetDisablePassThrough(bool disable) override
Disables AC3/DTS pass through.
QList< TrackType > stream_track_types
TrackType (608 or 708) for Captions seen in the caption stream itself but not seen in the PMT.
QString GetTrackDesc(uint type, uint trackNo) const override
long long lastKey
Definition: decoderbase.h:314
MythRenderVDPAU * getRender() const
int filter_max_ch(const AVFormatContext *ic, const sinfo_vec_t &tracks, const vector< int > &fs, enum AVCodecID codecId=AV_CODEC_ID_NONE, int profile=-1)
void GetAttachmentData(uint trackNo, QByteArray &filename, QByteArray &data) override
A QElapsedTimer based timer to replace use of QTime as a timer.
Definition: mythtimer.h:13
void SetFrameRate(double fps)
Definition: mythplayer.cpp:882
int64_t ptsdiff(uint64_t pts1, uint64_t pts2)
Definition: pes.c:78
int64_t LengthLastData(void)
static MythCodecID GetBestSupportedCodec(uint width, uint height, const QString &decoder, uint stream_type, bool no_acceleration)
#define FF_VDPAU_STATE_USED_FOR_REFERENCE
static int64_t lsb3full(int64_t lsb, int64_t base_ts, int lsb_bits)
void SetErrored(const QString &reason)
uint language_index
Definition: decoderbase.h:102
const DVDRingBuffer * DVD(void) const
virtual int BestBufferSize(void)
#define VBI_TYPE_VPS
Definition: ivtv_myth.h:276
static MythCodecID GetBestSupportedCodec(AVCodec **ppCodec, const QString &decoder, uint stream_type, AVPixelFormat &pix_fmt)
double fps
Definition: decoderbase.h:306
void DecodeDTVCC(const uint8_t *buf, uint buf_size, bool scte)
PlayerFlags
Definition: mythplayer.h:88
int cc608_parity_table[256]
VERBOSE_PREAMBLE Most true
Definition: verbosedefs.h:91
virtual void SeekReset(long long newkey, uint skipFrames, bool doFlush, bool discardFrames)
static uint64_t samples[4]
Definition: element.c:45
bool onKeyFrameStart(void) const
Definition: H264Parser.h:155
#define VBI_TYPE_WSS
Definition: ivtv_myth.h:275
static desc_list_t ParseOnlyInclude(const unsigned char *data, uint len, int descriptorid)
static int64_t AVF_Seek_Packet(void *opaque, int64_t offset, int whence)
virtual bool IsInDiscMenuOrStillFrame(void) const
EofState ateof
Definition: decoderbase.h:324
URLContext readcontext
bool justAfterChange
Definition: decoderbase.h:353
VideoOutput * GetVideoOutput(void)
Definition: mythplayer.h:244
uint32_t addBytes(const uint8_t *bytes, const uint32_t byte_count, const uint64_t