MythTV  master
thumbfinder.cpp
Go to the documentation of this file.
1 /* -*- Mode: c++ -*-
2  * vim: set expandtab tabstop=4 shiftwidth=4:
3  *
4  * Original Project
5  * MythTV http://www.mythtv.org
6  *
7  * Copyright (c) 2004, 2005 John Pullan <john@pullan.org>
8  * Copyright (c) 2009, Janne Grunau <janne-mythtv@grunau.be>
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
24  *
25  */
26 
27 // c++
28 #include <cerrno>
29 #include <cmath>
30 #include <cstdlib>
31 #include <iostream>
32 #include <sys/stat.h>
33 
34 // qt
35 #include <QApplication>
36 #include <QDomDocument>
37 #include <QFile>
38 #include <QDir>
39 #include <QPainter>
40 
41 // myth
42 #include <mythcontext.h>
43 #include <mythdbcon.h>
44 #include <programinfo.h>
45 #include <mythuihelper.h>
46 #include <mythmainwindow.h>
47 #include <mythdialogbox.h>
48 #include <mythdirs.h>
49 #include <mythmiscutil.h>
50 #include <mythuitext.h>
51 #include <mythuibutton.h>
52 #include <mythuiimage.h>
53 #include <mythuibuttonlist.h>
54 #include <mythimage.h>
55 #include <mythconfig.h>
56 
57 extern "C" {
58 #include "libavutil/imgutils.h"
59 }
60 
61 #ifndef INT64_C // Used in FFmpeg headers to define some constants
62 #define INT64_C(v) (v ## LL)
63 #endif
64 
65 // mytharchive
66 #include "thumbfinder.h"
67 
68 // the amount to seek before the required frame
69 #define PRE_SEEK_AMOUNT 50
70 
72 {
73  {"frame", -1},
74  {"1 second", 1},
75  {"5 seconds", 5},
76  {"10 seconds", 10},
77  {"30 seconds", 30},
78  {"1 minute", 60},
79  {"5 minutes", 300},
80  {"10 minutes", 600},
81  {"Cut Point", -2},
82 };
83 
84 int SeekAmountsCount = sizeof(SeekAmounts) / sizeof(SeekAmounts[0]);
85 
87  const QString &menuTheme)
88  :MythScreenType(parent, "ThumbFinder"),
89  m_archiveItem(archiveItem),
90  m_thumbCount(getChapterCount(menuTheme)),
91  m_thumbDir(createThumbDir())
92 {
93  // copy thumbList so we can abandon changes if required
94  m_thumbList.clear();
95  foreach (auto item, m_archiveItem->thumbList)
96  {
97  auto *thumb = new ThumbImage;
98  *thumb = *item;
99  m_thumbList.append(thumb);
100  }
101 }
102 
104 {
105  getThumbImages();
106 }
107 
109 {
110  while (!m_thumbList.isEmpty())
111  delete m_thumbList.takeFirst();
112  m_thumbList.clear();
113 
114  closeAVCodec();
115 }
116 
118 {
119  // Load the theme for this screen
120  bool foundtheme = LoadWindowFromXML("mythburn-ui.xml", "thumbfinder", this);
121  if (!foundtheme)
122  return false;
123 
124  bool err = false;
125  UIUtilE::Assign(this, m_frameImage, "frameimage", &err);
126  UIUtilE::Assign(this, m_positionImage, "positionimage", &err);
127  UIUtilE::Assign(this, m_imageGrid, "thumblist", &err);
128  UIUtilE::Assign(this, m_saveButton, "save_button", &err);
129  UIUtilE::Assign(this, m_cancelButton, "cancel_button", &err);
130  UIUtilE::Assign(this, m_frameButton, "frame_button", &err);
131  UIUtilE::Assign(this, m_seekAmountText, "seekamount", &err);
132  UIUtilE::Assign(this, m_currentPosText, "currentpos", &err);
133 
134  if (err)
135  {
136  LOG(VB_GENERAL, LOG_ERR, "Cannot load screen 'mythburn'");
137  return false;
138  }
139 
140  connect(m_imageGrid, SIGNAL(itemSelected(MythUIButtonListItem *)),
141  this, SLOT(gridItemChanged(MythUIButtonListItem *)));
142 
143  connect(m_saveButton, SIGNAL(Clicked()), this, SLOT(savePressed()));
144  connect(m_cancelButton, SIGNAL(Clicked()), this, SLOT(cancelPressed()));
145 
146  connect(m_frameButton, SIGNAL(Clicked()), this, SLOT(updateThumb()));
147 
149 
150  BuildFocusList();
151 
153 
154  return true;
155 }
156 
157 bool ThumbFinder::keyPressEvent(QKeyEvent *event)
158 {
159  if (GetFocusWidget()->keyPressEvent(event))
160  return true;
161 
162  QStringList actions;
163  bool handled = GetMythMainWindow()->TranslateKeyPress("Archive", event, actions);
164 
165  for (int i = 0; i < actions.size() && !handled; i++)
166  {
167  QString action = actions[i];
168  handled = true;
169 
170  if (action == "MENU")
171  {
172  NextPrevWidgetFocus(true);
173  return true;
174  }
175 
176  if (action == "ESCAPE")
177  {
178  ShowMenu();
179  return true;
180  }
181 
182  if (action == "0" || action == "1" || action == "2" || action == "3" ||
183  action == "4" || action == "5" || action == "6" || action == "7" ||
184  action == "8" || action == "9")
185  {
187  int itemNo = m_imageGrid->GetCurrentPos();
188  ThumbImage *thumb = m_thumbList.at(itemNo);
189  if (thumb)
190  seekToFrame(thumb->frame);
191  return true;
192  }
193 
194  if (GetFocusWidget() == m_frameButton)
195  {
196  if (action == "UP")
197  {
198  changeSeekAmount(true);
199  }
200  else if (action == "DOWN")
201  {
202  changeSeekAmount(false);
203  }
204  else if (action == "LEFT")
205  {
206  seekBackward();
207  }
208  else if (action == "RIGHT")
209  {
210  seekForward();
211  }
212  else if (action == "SELECT")
213  {
214  updateThumb();
215  }
216  else
217  handled = false;
218  }
219  else
220  handled = false;
221  }
222 
223  if (!handled && MythScreenType::keyPressEvent(event))
224  handled = true;
225 
226  return handled;
227 }
228 
229 int ThumbFinder::getChapterCount(const QString &menuTheme)
230 {
231  QString filename = GetShareDir() + "mytharchive/themes/" +
232  menuTheme + "/theme.xml";
233  QDomDocument doc("mydocument");
234  QFile file(filename);
235 
236  if (!file.open(QIODevice::ReadOnly))
237  {
238  LOG(VB_GENERAL, LOG_ERR, "Failed to open theme file: " + filename);
239  return 0; //??
240  }
241  if (!doc.setContent(&file))
242  {
243  file.close();
244  LOG(VB_GENERAL, LOG_ERR, "Failed to parse theme file: " + filename);
245  return 0;
246  }
247  file.close();
248 
249  QDomNodeList chapterNodeList = doc.elementsByTagName("chapter");
250 
251  return chapterNodeList.count();
252 }
253 
255 {
257 
258  if (progInfo && m_archiveItem->hasCutlist)
259  progInfo->QueryCutList(m_deleteMap);
260  delete progInfo;
261 
262  if (m_deleteMap.isEmpty())
263  {
264  LOG(VB_GENERAL, LOG_ERR, "ThumbFinder::loadCutList: Got an empty delete map");
265  return;
266  }
267 
268  // if the first mark is a end mark then add the start mark at the beginning
269  frm_dir_map_t::const_iterator it = m_deleteMap.begin();
270  if (it.value() == MARK_CUT_END)
271  m_deleteMap.insert(0, MARK_CUT_START);
272 
273 
274  // if the last mark is a start mark then add the end mark at the end
275  it = m_deleteMap.end();
276  --it;
277  if (it != m_deleteMap.end())
278  {
279  if (it.value() == MARK_CUT_START)
281  }
282 }
283 
285 {
286  // copy the thumb details to the archiveItem
287  while (!m_archiveItem->thumbList.isEmpty())
288  delete m_archiveItem->thumbList.takeFirst();
289  m_archiveItem->thumbList.clear();
290 
291  foreach (auto item, m_thumbList)
292  {
293  auto *thumb = new ThumbImage;
294  *thumb = *item;
295  m_archiveItem->thumbList.append(thumb);
296  }
297 
298  Close();
299 }
300 
302 {
303  Close();
304 }
305 
307 {
308  int64_t pos = m_currentPTS - m_firstIFramePTS;
309  int64_t frame = pos / m_frameTime;
310 
311  if (m_currentPosText)
312  m_currentPosText->SetText(frameToTime(frame, true));
313 
314  updatePositionBar(frame);
315 }
316 
318 {
319  if (up)
320  {
321  m_currentSeek++;
323  m_currentSeek = 0;
324  }
325  else
326  {
327  m_currentSeek--;
328  if (m_currentSeek < 0)
330  }
331 
333 }
334 
336 {
337  (void) item;
338 
339  int itemNo = m_imageGrid->GetCurrentPos();
340  ThumbImage *thumb = m_thumbList.at(itemNo);
341  if (thumb)
342  seekToFrame(thumb->frame);
343 }
344 
346 {
347  QString thumbDir = getTempDirectory() + "config/thumbs";
348 
349  // make sure the thumb directory exists
350  QDir dir(thumbDir);
351  if (!dir.exists())
352  {
353  dir.mkdir(thumbDir);
354  if( chmod(qPrintable(thumbDir), 0777) != 0 )
355  LOG(VB_GENERAL, LOG_ERR, "ThumbFinder: Failed to change permissions"
356  " on thumb directory: " + ENO);
357  }
358 
359  QString path;
360  for (int x = 1; dir.exists(); x++)
361  {
362  path = thumbDir + QString("/%1").arg(x);
363  dir.setPath(path);
364  }
365 
366  dir.mkdir(path);
367  if( chmod(qPrintable(path), 0777) != 0 )
368  LOG(VB_GENERAL, LOG_ERR, "ThumbFinder: Failed to change permissions on "
369  "thumb directory: %1" + ENO);
370 
371  return path;
372 }
373 
375 {
376  int itemNo = m_imageGrid->GetCurrentPos();
378 
379  ThumbImage *thumb = m_thumbList.at(itemNo);
380  if (!thumb)
381  return;
382 
383  // copy current frame image to the selected thumb image
384  QString imageFile = thumb->filename;
385  QFile dst(imageFile);
386  QFile src(m_frameFile);
387  copy(dst, src);
388 
389  item->SetImage(imageFile, "", true);
390 
391  // update the image grid item
392  int64_t pos = (int) ((m_currentPTS - m_startPTS) / m_frameTime);
393  thumb->frame = pos - m_offset;
394  if (itemNo != 0)
395  {
396  thumb->caption = frameToTime(thumb->frame);
397  item->SetText(thumb->caption);
398  }
399 
401 }
402 
403 QString ThumbFinder::frameToTime(int64_t frame, bool addFrame)
404 {
405  QString str;
406 
407  int sec = (int) (frame / m_fps);
408  frame = frame - (int) (sec * m_fps);
409  int min = sec / 60;
410  sec %= 60;
411  int hour = min / 60;
412  min %= 60;
413 
414  if (addFrame)
415  str = str.sprintf("%01d:%02d:%02d.%02d", hour, min, sec, (int) frame);
416  else
417  str = str.sprintf("%02d:%02d:%02d", hour, min, sec);
418  return str;
419 }
420 
422 {
424  {
425  LOG(VB_GENERAL, LOG_ERR,
426  QString("ThumbFinder:: Failed to get file details for %1")
427  .arg(m_archiveItem->filename));
428  return false;
429  }
430 
432  return false;
433 
434  if (m_archiveItem->type == "Recording")
435  loadCutList();
436 
437  // calculate the file duration taking the cut list into account
439 
440  QString origFrameFile = m_frameFile;
441 
442  m_updateFrame = true;
443  getFrameImage();
444 
445  int chapterLen = 0;
446  if (m_thumbCount)
447  chapterLen = m_finalDuration / m_thumbCount;
448  else
449  chapterLen = m_finalDuration;
450 
451  QString thumbList = "";
452  m_updateFrame = false;
453 
454  // add title thumb
455  m_frameFile = m_thumbDir + "/title.jpg";
456  ThumbImage *thumb = nullptr;
457 
458  if (!m_thumbList.empty())
459  {
460  // use the thumb details in the thumbList if already available
461  thumb = m_thumbList.at(0);
462  }
463 
464  if (!thumb)
465  {
466  // no thumb available create a new one
467  thumb = new ThumbImage;
468  thumb->filename = m_frameFile;
469  thumb->frame = (int64_t) 0;
470  thumb->caption = "Title";
471  m_thumbList.append(thumb);
472  }
473  else
474  m_frameFile = thumb->filename;
475 
476  seekToFrame(thumb->frame);
477  getFrameImage();
478 
479  new MythUIButtonListItem(m_imageGrid, thumb->caption, thumb->filename);
480 
481  qApp->processEvents();
482 
483  for (int x = 1; x <= m_thumbCount; x++)
484  {
485  m_frameFile = m_thumbDir + QString("/chapter-%1.jpg").arg(x);
486 
487  thumb = nullptr;
488 
489  if (m_archiveItem->thumbList.size() > x)
490  {
491  // use the thumb details in the archiveItem if already available
492  thumb = m_archiveItem->thumbList.at(x);
493  }
494 
495  if (!thumb)
496  {
497  int chapter = chapterLen * (x - 1);
498  int hour = chapter / 3600;
499  int min = (chapter % 3600) / 60;
500  int sec = chapter % 60;
501  QString time = QString::asprintf("%02d:%02d:%02d", hour, min, sec);
502 
503  auto frame = (int64_t) (chapter * ceil(m_fps));
504 
505  // no thumb available create a new one
506  thumb = new ThumbImage;
507  thumb->filename = m_frameFile;
508  thumb->frame = frame;
509  thumb->caption = time;
510  m_thumbList.append(thumb);
511  }
512  else
513  m_frameFile = thumb->filename;
514 
515  seekToFrame(thumb->frame);
516  qApp->processEvents();
517  getFrameImage();
518  qApp->processEvents();
519  new MythUIButtonListItem(m_imageGrid, thumb->caption, thumb->filename);
520  qApp->processEvents();
521  }
522 
523  m_frameFile = origFrameFile;
524  seekToFrame(0);
525 
526  m_updateFrame = true;
527 
529 
531 
532  return true;
533 }
534 
535 bool ThumbFinder::initAVCodec(const QString &inFile)
536 {
537  // Open recording
538  LOG(VB_JOBQUEUE, LOG_INFO, QString("ThumbFinder: Opening '%1'")
539  .arg(inFile));
540 
541  if (!m_inputFC.Open(inFile))
542  {
543  LOG(VB_GENERAL, LOG_ERR, "ThumbFinder, Couldn't open input file" + ENO);
544  return false;
545  }
546 
547  // Getting stream information
548  int ret = avformat_find_stream_info(m_inputFC, nullptr);
549  if (ret < 0)
550  {
551  LOG(VB_GENERAL, LOG_ERR,
552  QString("Couldn't get stream info, error #%1").arg(ret));
553  return false;
554  }
555 
556  av_dump_format(m_inputFC, 0, qPrintable(inFile), 0);
557 
558  // find the first video stream
559  m_videostream = -1;
560 
561  for (uint i = 0; i < m_inputFC->nb_streams; i++)
562  {
563  AVStream *st = m_inputFC->streams[i];
564  if (m_inputFC->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
565  {
566  m_startTime = -1;
567  if (m_inputFC->streams[i]->start_time != (int) AV_NOPTS_VALUE)
568  m_startTime = m_inputFC->streams[i]->start_time;
569  else
570  {
571  LOG(VB_GENERAL, LOG_ERR,
572  "ThumbFinder: Failed to get start time");
573  return false;
574  }
575 
576  m_videostream = i;
577  m_frameWidth = st->codecpar->width;
578  m_frameHeight = st->codecpar->height;
579  if (st->r_frame_rate.den && st->r_frame_rate.num)
580  m_fps = av_q2d(st->r_frame_rate);
581  else
582  m_fps = 1/av_q2d(st->time_base);
583  break;
584  }
585  }
586 
587  if (m_videostream == -1)
588  {
589  LOG(VB_GENERAL, LOG_ERR, "Couldn't find a video stream");
590  return false;
591  }
592 
593  // get the codec context for the video stream
595  m_codecCtx->debug_mv = 0;
596  m_codecCtx->debug = 0;
597  m_codecCtx->workaround_bugs = 1;
598  m_codecCtx->lowres = 0;
599  m_codecCtx->idct_algo = FF_IDCT_AUTO;
600  m_codecCtx->skip_frame = AVDISCARD_DEFAULT;
601  m_codecCtx->skip_idct = AVDISCARD_DEFAULT;
602  m_codecCtx->skip_loop_filter = AVDISCARD_DEFAULT;
603  m_codecCtx->err_recognition = AV_EF_CAREFUL;
604  m_codecCtx->error_concealment = 3;
605 
606  // get decoder for video stream
607  m_codec = avcodec_find_decoder(m_codecCtx->codec_id);
608 
609  if (m_codec == nullptr)
610  {
611  LOG(VB_GENERAL, LOG_ERR,
612  "ThumbFinder: Couldn't find codec for video stream");
613  return false;
614  }
615 
616  // open codec
617  if (avcodec_open2(m_codecCtx, m_codec, nullptr) < 0)
618  {
619  LOG(VB_GENERAL, LOG_ERR,
620  "ThumbFinder: Couldn't open codec for video stream");
621  return false;
622  }
623 
624  // allocate temp buffer
625  int bufflen = m_frameWidth * m_frameHeight * 4;
626  m_outputbuf = new unsigned char[bufflen];
627 
628  m_frameFile = getTempDirectory() + "work/frame.jpg";
629 
632  return true;
633 }
634 
636 {
637  if (m_deleteMap.isEmpty() || !m_archiveItem->useCutlist)
638  return frameNumber;
639 
640  int diff = 0;
641  frm_dir_map_t::const_iterator it = m_deleteMap.find(frameNumber);
642 
643  for (it = m_deleteMap.begin(); it != m_deleteMap.end(); ++it)
644  {
645  int start = it.key();
646 
647  ++it;
648  if (it == m_deleteMap.end())
649  {
650  LOG(VB_GENERAL, LOG_ERR, "ThumbFinder: found a start cut but no cut end");
651  break;
652  }
653 
654  int end = it.key();
655 
656  if (start <= frameNumber + diff)
657  diff += end - start;
658  }
659 
660  m_offset = diff;
661  return frameNumber + diff;
662 }
663 
664 bool ThumbFinder::seekToFrame(int frame, bool checkPos)
665 {
666  // make sure the frame is not in a cut point
667  if (checkPos)
668  frame = checkFramePosition(frame);
669 
670  // seek to a position PRE_SEEK_AMOUNT frames before the required frame
671  int64_t timestamp = m_startTime + (frame * m_frameTime) -
673  int64_t requiredPTS = m_startPTS + (frame * m_frameTime);
674 
675  if (timestamp < m_startTime)
676  timestamp = m_startTime;
677 
678  if (av_seek_frame(m_inputFC, m_videostream, timestamp, AVSEEK_FLAG_ANY) < 0)
679  {
680  LOG(VB_GENERAL, LOG_ERR, "ThumbFinder::SeekToFrame: seek failed") ;
681  return false;
682  }
683 
684  avcodec_flush_buffers(m_codecCtx);
685  getFrameImage(true, requiredPTS);
686 
687  return true;
688 }
689 
691 {
692  int64_t currentFrame = (m_currentPTS - m_startPTS) / m_frameTime;
693 
694  int inc = SeekAmounts[m_currentSeek].amount;
695 
696  if (inc == -1)
697  inc = 1;
698  else if (inc == -2)
699  {
700  int pos = 0;
701  frm_dir_map_t::const_iterator it;
702  for (it = m_deleteMap.begin(); it != m_deleteMap.end(); ++it)
703  {
704  if (it.key() > (uint64_t)currentFrame)
705  {
706  pos = it.key();
707  break;
708  }
709  }
710  // seek to next cutpoint
711  m_offset = 0;
712  seekToFrame(pos, false);
713  return true;
714  }
715  else
716  inc = (int) (inc * ceil(m_fps));
717 
718  int64_t newFrame = currentFrame + inc - m_offset;
719  if (newFrame == currentFrame + 1)
720  getFrameImage(false);
721  else
722  seekToFrame(newFrame);
723 
724  return true;
725 }
726 
728 {
729  int64_t currentFrame = (m_currentPTS - m_startPTS) / m_frameTime;
730 
731  int inc = SeekAmounts[m_currentSeek].amount;
732  if (inc == -1)
733  inc = -1;
734  else if (inc == -2)
735  {
736  // seek to previous cut point
737  frm_dir_map_t::const_iterator it;
738  int pos = 0;
739  for (it = m_deleteMap.begin(); it != m_deleteMap.end(); ++it)
740  {
741  if (it.key() >= (uint64_t)currentFrame)
742  break;
743 
744  pos = it.key();
745  }
746 
747  // seek to next cutpoint
748  m_offset = 0;
749  seekToFrame(pos, false);
750  return true;
751  }
752  else
753  inc = (int) (-inc * ceil(m_fps));
754 
755  int64_t newFrame = currentFrame + inc - m_offset;
756  seekToFrame(newFrame);
757 
758  return true;
759 }
760 
761 bool ThumbFinder::getFrameImage(bool needKeyFrame, int64_t requiredPTS)
762 {
763  AVPacket pkt;
764  AVFrame orig;
765  AVFrame retbuf;
766  memset(&orig, 0, sizeof(AVFrame));
767  memset(&retbuf, 0, sizeof(AVFrame));
768 
769  av_init_packet(&pkt);
770 
771  bool frameFinished = false;
772  int frameCount = 0;
773  bool gotKeyFrame = false;
774 
775  while (av_read_frame(m_inputFC, &pkt) >= 0 && !frameFinished)
776  {
777  if (pkt.stream_index == m_videostream)
778  {
779  frameCount++;
780 
781  int keyFrame = pkt.flags & AV_PKT_FLAG_KEY;
782 
783  if (m_startPTS == -1 && pkt.dts != AV_NOPTS_VALUE)
784  {
785  m_startPTS = pkt.dts;
786  m_frameTime = pkt.duration;
787  }
788 
789  if (keyFrame)
790  gotKeyFrame = true;
791 
792  if (!gotKeyFrame && needKeyFrame)
793  {
794  av_packet_unref(&pkt);
795  continue;
796  }
797 
798  if (m_firstIFramePTS == -1)
799  m_firstIFramePTS = pkt.dts;
800 
801  av_frame_unref(m_frame);
802  frameFinished = false;
803  int ret = avcodec_receive_frame(m_codecCtx, m_frame);
804  if (ret == 0)
805  frameFinished = true;
806  if (ret == 0 || ret == AVERROR(EAGAIN))
807  avcodec_send_packet(m_codecCtx, &pkt);
808  if (requiredPTS != -1 && pkt.dts != AV_NOPTS_VALUE && pkt.dts < requiredPTS)
809  frameFinished = false;
810 
811  m_currentPTS = pkt.dts;
812  }
813 
814  av_packet_unref(&pkt);
815  }
816 
817  if (frameFinished)
818  {
819  av_image_fill_arrays(retbuf.data, retbuf.linesize, m_outputbuf,
820  AV_PIX_FMT_RGB32, m_frameWidth, m_frameHeight, IMAGE_ALIGN);
821  AVFrame *tmp = m_frame;
822 
823  m_deinterlacer->DeinterlaceSingle(tmp, tmp);
824 
825  m_copy.Copy(&retbuf, AV_PIX_FMT_RGB32, tmp, m_codecCtx->pix_fmt,
827 
829  QImage::Format_RGB32);
830 
831  QByteArray ffile = m_frameFile.toLocal8Bit();
832  if (!img.save(ffile.constData(), "JPEG"))
833  {
834  LOG(VB_GENERAL, LOG_ERR, "Failed to save thumb: " + m_frameFile);
835  }
836 
837  if (m_updateFrame)
838  {
839  MythImage *mimage =
841  mimage->Assign(img);
842  m_frameImage->SetImage(mimage);
843  mimage->DecrRef();
844  }
845 
847  }
848 
849  return true;
850 }
851 
853 {
854  delete[] m_outputbuf;
855 
856  // close the codec
858 
859  // close the video file
860  m_inputFC.Close();
861 }
862 
864 {
865  MythScreenStack *popupStack = GetMythMainWindow()->GetStack("popup stack");
866  auto *menuPopup = new MythDialogBox(tr("Menu"), popupStack, "actionmenu");
867 
868  if (menuPopup->Create())
869  popupStack->AddScreen(menuPopup);
870 
871  menuPopup->SetReturnEvent(this, "action");
872 
873  menuPopup->AddButton(tr("Exit, Save Thumbnails"), SLOT(savePressed()));
874  menuPopup->AddButton(tr("Exit, Don't Save Thumbnails"), SLOT(cancelPressed()));
875 }
876 
878 {
879  if (!m_positionImage)
880  return;
881 
882  QSize size = m_positionImage->GetArea().size();
883  auto *pixmap = new QPixmap(size.width(), size.height());
884 
885  QPainter p(pixmap);
886  QBrush brush(Qt::green);
887 
888  p.setBrush(brush);
889  p.setPen(Qt::NoPen);
890  p.fillRect(0, 0, size.width(), size.height(), brush);
891 
892  frm_dir_map_t::const_iterator it;
893 
894  brush.setColor(Qt::red);
895 
896  for (it = m_deleteMap.begin(); it != m_deleteMap.end(); ++it)
897  {
898  double startdelta = size.width();
899  if (it.key() != 0)
900  startdelta = (m_archiveItem->duration * m_fps) / it.key();
901 
902  ++it;
903  if (it == m_deleteMap.end())
904  {
905  LOG(VB_GENERAL, LOG_ERR, "ThumbFinder: found a start cut but no cut end");
906  break;
907  }
908 
909  double enddelta = size.width();
910  if (it.key() != 0)
911  enddelta = (m_archiveItem->duration * m_fps) / it.key();
912 
913  int start = (int) (size.width() / startdelta);
914  int end = (int) (size.width() / enddelta);
915  p.fillRect(start - 1, 0, end - start, size.height(), brush);
916  }
917 
918  if (frame == 0)
919  frame = 1;
920  brush.setColor(Qt::yellow);
921  int pos = (int) (size.width() / ((m_archiveItem->duration * m_fps) / frame));
922  p.fillRect(pos, 0, 3, size.height(), brush);
923 
925  image->Assign(*pixmap);
926  m_positionImage->SetImage(image);
927 
928  p.end();
929  delete pixmap;
930 }
931 
933 {
934  if (m_archiveItem->type == "Recording")
935  {
937  {
938  frm_dir_map_t::const_iterator it;
939 
940  int cutLen = 0;
941 
942  for (it = m_deleteMap.begin(); it != m_deleteMap.end(); ++it)
943  {
944  int start = it.key();
945 
946  ++it;
947  if (it == m_deleteMap.end())
948  {
949  LOG(VB_GENERAL, LOG_ERR, "ThumbFinder: found a start cut but no cut end");
950  break;
951  }
952 
953  int end = it.key();
954  cutLen += end - start;
955  }
956  return m_archiveItem->duration - (int) (cutLen / m_fps);
957  }
958  }
959 
960  return m_archiveItem->duration;
961 }
void updateCurrentPos(void)
QString filename
Definition: archiveutil.h:47
struct SeekAmount SeekAmounts[]
Definition: thumbfinder.cpp:71
MythAVCopy m_copy
Definition: thumbfinder.h:86
int m_videostream
Definition: thumbfinder.h:94
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
void SetImage(MythImage *img)
Should not be used unless absolutely necessary since it bypasses the image caching and threaded loade...
QString caption
Definition: archiveutil.h:46
bool getThumbImages(void)
QString frameToTime(int64_t frame, bool addFrame=false)
void cancelPressed(void)
int64_t m_currentPTS
Definition: thumbfinder.h:98
bool TranslateKeyPress(const QString &context, QKeyEvent *e, QStringList &actions, bool allowJumps=true)
Get a list of actions for a keypress in the given context.
int Copy(VideoFrame *dst, const VideoFrame *src)
Definition: mythavutil.cpp:330
QString getTempDirectory(bool showError)
Definition: archiveutil.cpp:71
AVCodecContext * m_codecCtx
Definition: thumbfinder.h:82
QString m_frameFile
Definition: thumbfinder.h:91
bool hasCutlist
Definition: archiveutil.h:70
void SetRedraw(void)
Definition: mythuitype.cpp:293
int DecrRef(void) override
Decrements reference count and deletes on 0.
Definition: mythimage.cpp:55
bool useCutlist
Definition: archiveutil.h:71
Basic menu dialog, message and a list of options.
int m_frameWidth
Definition: thumbfinder.h:92
ArchiveItem * m_archiveItem
Definition: thumbfinder.h:106
struct AVFrame AVFrame
virtual void SetText(const QString &text)
Definition: mythuitext.cpp:135
bool Create(void) override
MythScreenStack * GetStack(const QString &stackname)
int64_t m_startTime
Definition: thumbfinder.h:96
#define PRE_SEEK_AMOUNT
Definition: thumbfinder.cpp:69
QString name
Definition: thumbfinder.h:24
bool getFileDetails(ArchiveItem *a)
long long copy(QFile &dst, QFile &src, uint block_size)
Copies src file to dst file.
void changeSeekAmount(bool up)
void freeCodecContext(const AVStream *stream)
Definition: mythavutil.cpp:554
frm_dir_map_t m_deleteMap
Definition: thumbfinder.h:102
static guint32 * tmp
Definition: goom_core.c:35
MythAVFrame m_frame
Definition: thumbfinder.h:85
void BuildFocusList(void)
void SetImage(MythImage *image, const QString &name="")
Sets an image directly, should only be used in special circumstances since it bypasses the cache.
qint64 frame
Definition: archiveutil.h:48
int64_t m_firstIFramePTS
Definition: thumbfinder.h:99
QString filename
Definition: archiveutil.h:60
virtual void Close()
void Init(void) override
Used after calling Load() to assign data to widgets and other UI initilisation which is prohibited in...
Holds information on recordings and videos.
Definition: programinfo.h:67
MythUIButton * m_frameButton
Definition: thumbfinder.h:112
virtual void AddScreen(MythScreenType *screen, bool allowFade=true)
int m_currentSeek
Definition: thumbfinder.h:95
int checkFramePosition(int frameNumber)
MythUIButtonList * m_imageGrid
Definition: thumbfinder.h:117
int SeekAmountsCount
Definition: thumbfinder.cpp:84
int calcFinalDuration(void)
bool initAVCodec(const QString &inFile)
AVCodecContext * getCodecContext(const AVStream *stream, const AVCodec *pCodec=nullptr, bool nullCodec=false)
Definition: mythavutil.cpp:512
virtual MythRect GetArea(void) const
If the object has a minimum area defined, return it, other wise return the default area.
Definition: mythuitype.cpp:864
virtual bool NextPrevWidgetFocus(bool up_or_down)
QString GetShareDir(void)
Definition: mythdirs.cpp:222
MythUIButton * m_saveButton
Definition: thumbfinder.h:113
bool getFrameImage(bool needKeyFrame=true, int64_t requiredPTS=-1)
RemoteAVFormatContext m_inputFC
Definition: thumbfinder.h:81
void gridItemChanged(MythUIButtonListItem *item)
bool seekToFrame(int frame, bool checkPos=true)
static bool Assign(ContainerType *container, UIType *&item, const QString &name, bool *err=nullptr)
Definition: mythuiutils.h:27
void SetText(const QString &text, const QString &name="", const QString &state="")
unsigned int uint
Definition: compat.h:140
MythPainter * GetCurrentPainter()
bool m_updateFrame
Definition: thumbfinder.h:101
#define ENO
This can be appended to the LOG args with "+".
Definition: mythlogging.h:99
QScopedPointer< MythPictureDeinterlacer > m_deinterlacer
Definition: thumbfinder.h:87
static bool LoadWindowFromXML(const QString &xmlfile, const QString &windowname, MythUIType *parent)
QString m_thumbDir
Definition: thumbfinder.h:109
MythUIType * GetFocusWidget(void) const
QString type
Definition: archiveutil.h:54
ThumbFinder(MythScreenStack *parent, ArchiveItem *archiveItem, const QString &menuTheme)
Definition: thumbfinder.cpp:86
void ShowMenu(void) override
MythMainWindow * GetMythMainWindow(void)
void savePressed(void)
bool keyPressEvent(QKeyEvent *event) override
Key event handler.
MythPictureDeinterlacer simple deinterlacer based on FFmpeg's yadif filter.
Definition: mythavutil.h:168
void loadCutList(void)
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
MythUIImage * m_positionImage
Definition: thumbfinder.h:116
float m_fps
Definition: thumbfinder.h:89
void closeAVCodec()
MythUIButton * m_cancelButton
Definition: thumbfinder.h:114
MythUIText * m_currentPosText
Definition: thumbfinder.h:119
void updateThumb(void)
static QString createThumbDir(void)
~ThumbFinder() override
MythCodecMap m_codecMap
Definition: thumbfinder.h:83
static int getChapterCount(const QString &menuTheme)
QList< ThumbImage * > m_thumbList
Definition: thumbfinder.h:108
void SetItemCurrent(MythUIButtonListItem *item)
MythUIImage * m_frameImage
Definition: thumbfinder.h:115
bool SetFocusWidget(MythUIType *widget=nullptr)
int m_thumbCount
Definition: thumbfinder.h:107
ProgramInfo * getProgramInfoForFile(const QString &inFile)
unsigned char * m_outputbuf
Definition: thumbfinder.h:90
Screen in which all other widgets are contained and rendered.
void Assign(const QImage &img)
Definition: mythimage.cpp:80
int m_frameHeight
Definition: thumbfinder.h:93
int GetCurrentPos() const
AVCodec * m_codec
Definition: thumbfinder.h:84
MythImage * GetFormatImage()
Returns a blank reference counted image in the format required for the Draw functions for this painte...
bool QueryCutList(frm_dir_map_t &delMap, bool loadAutosave=false) const
int64_t m_startPTS
Definition: thumbfinder.h:97
QList< ThumbImage * > thumbList
Definition: archiveutil.h:73
bool seekForward()
bool seekBackward()
void updatePositionBar(int64_t frame)
MythUIText * m_seekAmountText
Definition: thumbfinder.h:118
MythUIButtonListItem * GetItemCurrent() const
int m_finalDuration
Definition: thumbfinder.h:103