forked from wang-bin/QtAV
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add methods for VideoFrame to get a image or frame with another format
old convertTo() is a bad idea because VideoFrame now is explicitly shared and will be removed
- Loading branch information
Showing
6 changed files
with
82 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/****************************************************************************** | ||
ImageConverter: Base class for image resizing & color model convertion | ||
Copyright (C) 2012-2014 Wang Bin <[email protected]> | ||
Copyright (C) 2012-2015 Wang Bin <[email protected]> | ||
* This file is part of QtAV | ||
|
@@ -76,6 +76,7 @@ void ImageConverter::setInSize(int width, int height) | |
prepareData(); | ||
} | ||
|
||
// TODO: default is in size | ||
void ImageConverter::setOutSize(int width, int height) | ||
{ | ||
DPTR_D(ImageConverter); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/****************************************************************************** | ||
QtAV: Media play library based on Qt and FFmpeg | ||
Copyright (C) 2012-2014 Wang Bin <[email protected]> | ||
Copyright (C) 2012-2015 Wang Bin <[email protected]> | ||
* This file is part of QtAV | ||
|
@@ -83,11 +83,20 @@ class Q_AV_EXPORT VideoFrame : public Frame | |
|
||
//use ptr instead of ImageConverterId to avoid allocating memory | ||
// Id can be used in VideoThread | ||
void setImageConverter(ImageConverter *conv); | ||
void setImageConverter(ImageConverter *conv); // deprecated | ||
// if use gpu to convert, mapToDevice() first | ||
/*! | ||
* \brief toImage | ||
* Return a QImage of current video frame, with given format, image size and region of interest. | ||
* \param dstSize result image size | ||
* \param roi NOT implemented! | ||
*/ | ||
QImage toImage(QImage::Format fmt = QImage::Format_ARGB32, const QSize& dstSize = QSize(), const QRectF& roi = QRect()) const; | ||
VideoFrame toFormat(VideoFormat::PixelFormat pixfmt, const QSize& dstSize = QSize(), const QRectF& roi = QRect()) const; | ||
VideoFrame toFormat(const VideoFormat& fmt, const QSize& dstSize = QSize(), const QRectF& roi = QRect()) const; | ||
/*! | ||
* \brief convertTo | ||
* You may clone the frame first because VideoFrame is explicitly shared | ||
* deprecated. You may clone the frame first because VideoFrame is explicitly shared | ||
*/ | ||
bool convertTo(const VideoFormat& fmt); | ||
bool convertTo(VideoFormat::PixelFormat fmt); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/****************************************************************************** | ||
VideoCapture.cpp: description | ||
Copyright (C) 2012-2014 Wang Bin <[email protected]> | ||
Copyright (C) 2012-2015 Wang Bin <[email protected]> | ||
* This file is part of QtAV | ||
|
@@ -21,7 +21,6 @@ | |
|
||
|
||
#include "QtAV/VideoCapture.h" | ||
#include "QtAV/ImageConverterTypes.h" | ||
#include <QtCore/QCoreApplication> | ||
#include <QtCore/QDir> | ||
#include <QtCore/QRunnable> | ||
|
@@ -56,29 +55,12 @@ class CaptureTask : public QRunnable | |
qDebug("app is dieing. cancel capture task %p", this); | ||
return; | ||
} | ||
// TODO: add VideoFrame::toImage(QImage::Format),toFormat(VideoFormat)... | ||
ImageConverter *conv = ImageConverterFactory::create(ImageConverterId_FF); | ||
const VideoFormat vformat(frame.format()); | ||
conv->setInFormat(vformat.pixelFormatFFmpeg()); | ||
conv->setOutFormat(VideoFormat::pixelFormatToFFmpeg(VideoFormat::pixelFormatFromImageFormat(qfmt))); | ||
conv->setInSize(frame.width(), frame.height()); | ||
conv->setOutSize(frame.width(), frame.height()); | ||
const int nb_planes = vformat.planeCount(); | ||
QVector<uchar*> planes(nb_planes); | ||
QVector<int> line_sizes(nb_planes); | ||
for (int i = 0; i < nb_planes; ++i) { | ||
planes[i] = frame.bits(i); | ||
line_sizes[i] = frame.bytesPerLine(i); | ||
} | ||
QImage image; | ||
if (conv->convert(planes.constData(), line_sizes.constData())) { | ||
image = QImage((const uchar*)conv->outData().constData(), frame.width(), frame.height(), conv->outLineSizes().at(0), qfmt); | ||
image = image.copy(); | ||
QMetaObject::invokeMethod(cap, "imageCaptured", Q_ARG(QImage, image)); | ||
} else { | ||
QImage image(frame.toImage()); | ||
if (image.isNull()) { | ||
qWarning("Failed to convert to QImage"); | ||
return; | ||
} | ||
delete conv; | ||
QMetaObject::invokeMethod(cap, "imageCaptured", Q_ARG(QImage, image)); | ||
if (!save) | ||
return; | ||
bool main_thread = QThread::currentThread() == qApp->thread(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/****************************************************************************** | ||
QtAV: Media play library based on Qt and FFmpeg | ||
Copyright (C) 2012-2014 Wang Bin <[email protected]> | ||
Copyright (C) 2012-2015 Wang Bin <[email protected]> | ||
* This file is part of QtAV | ||
|
@@ -27,6 +27,7 @@ | |
#include "QtAV/private/AVCompat.h" | ||
#include <QtCore/QSharedPointer> | ||
#include <QtGui/QImage> | ||
#include "utils/Logger.h" | ||
|
||
// FF_API_PIX_FMT | ||
#ifdef PixelFormat | ||
|
@@ -387,6 +388,64 @@ bool VideoFrame::convertTo(const VideoFormat& fmt, const QSizeF &dstSize, const | |
return d_func()->convertTo(fmt, dstSize, roi); | ||
} | ||
|
||
QImage VideoFrame::toImage(QImage::Format fmt, const QSize& dstSize, const QRectF &roi) const | ||
{ | ||
Q_UNUSED(dstSize); | ||
Q_UNUSED(roi); | ||
if (!isValid() || !bits(0)) // only in data in host memory is supported now | ||
return QImage(); | ||
if (imageFormat() == fmt) { | ||
return QImage((const uchar*)frameData().constData(), width(), height(), bytesPerLine(0), fmt).copy(); | ||
} | ||
Q_D(const VideoFrame); | ||
QScopedPointer<ImageConverter> conv(ImageConverterFactory::create(ImageConverterId_FF)); | ||
conv->setInFormat(pixelFormatFFmpeg()); | ||
conv->setOutFormat(VideoFormat::pixelFormatToFFmpeg(VideoFormat::pixelFormatFromImageFormat(fmt))); | ||
conv->setInSize(width(), height()); | ||
if (!dstSize.isEmpty()) | ||
conv->setOutSize(dstSize.width(), dstSize.height()); | ||
else | ||
conv->setOutSize(width(), height()); | ||
if (!conv->convert(d->planes.constData(), d->line_sizes.constData())) { | ||
qWarning("VideoFrame::toImage error"); | ||
return QImage(); | ||
} | ||
QImage image((const uchar*)conv->outData().constData(), width(), height(), conv->outLineSizes().at(0), fmt); | ||
return image.copy(); | ||
} | ||
|
||
VideoFrame VideoFrame::toFormat(const VideoFormat &fmt, const QSize& dstSize, const QRectF& roi) const | ||
{ | ||
Q_UNUSED(dstSize); | ||
Q_UNUSED(roi); | ||
if (!isValid() || !bits(0)) // only in data in host memory is supported now | ||
return VideoFrame(); | ||
if (fmt.pixelFormatFFmpeg() == pixelFormatFFmpeg()) | ||
return clone(); | ||
Q_D(const VideoFrame); | ||
QScopedPointer<ImageConverter> conv(ImageConverterFactory::create(ImageConverterId_FF)); | ||
conv->setInFormat(pixelFormatFFmpeg()); | ||
conv->setOutFormat(fmt.pixelFormatFFmpeg()); | ||
conv->setInSize(width(), height()); | ||
if (!dstSize.isEmpty()) | ||
conv->setOutSize(dstSize.width(), dstSize.height()); | ||
else | ||
conv->setOutSize(width(), height()); | ||
if (!conv->convert(d->planes.constData(), d->line_sizes.constData())) { | ||
qWarning() << "VideoFrame::toFormat error: " << format() << "=>" << fmt; | ||
return VideoFrame(); | ||
} | ||
VideoFrame f(conv->outData(), width(), height(), fmt); | ||
f.setBits(conv->outPlanes()); | ||
f.setBytesPerLine(conv->outLineSizes()); | ||
return f; | ||
} | ||
|
||
VideoFrame VideoFrame::toFormat(VideoFormat::PixelFormat pixfmt, const QSize& dstSize, const QRectF &roi) const | ||
{ | ||
return toFormat(VideoFormat(pixfmt), dstSize, roi); | ||
} | ||
|
||
void *VideoFrame::map(SurfaceType type, void *handle, int plane) | ||
{ | ||
Q_D(VideoFrame); | ||
|