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