Skip to content

Commit

Permalink
ImageConverter.outData() must return shared instance. wang-bin#1046
Browse files Browse the repository at this point in the history
several apis are added, to avoid deep copy
  • Loading branch information
wang-bin committed Feb 8, 2018
1 parent e26096e commit f3ad0a5
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 15 deletions.
7 changes: 6 additions & 1 deletion src/Frame.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/******************************************************************************
QtAV: Multimedia framework based on Qt and FFmpeg
Copyright (C) 2012-2016 Wang Bin <[email protected]>
Copyright (C) 2012-2018 Wang Bin <[email protected]>
* This file is part of QtAV
Expand Down Expand Up @@ -59,6 +59,11 @@ QByteArray Frame::frameData() const
return d_func()->data;
}

int Frame::dataAlignment() const
{
return d_func()->data_align;
}

QByteArray Frame::data(int plane) const
{
if (plane < 0 || plane >= planeCount()) {
Expand Down
6 changes: 3 additions & 3 deletions src/ImageConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ ImageConverter::~ImageConverter()
QByteArray ImageConverter::outData() const
{
DPTR_D(const ImageConverter);
return QByteArray::fromRawData(d.data_out.constData()+d.out_offset, d.data_out.size() - d.out_offset);
return d.data_out;
}

bool ImageConverter::check() const
Expand Down Expand Up @@ -216,8 +216,8 @@ bool ImageConverter::prepareData()
const int nb_planes = qMax(av_pix_fmt_count_planes(d.fmt_out), 0);
d.bits.resize(nb_planes);
d.pitchs.resize(nb_planes);
// alignment is 16. sws in ffmpeg is 16, libav10 is 8
const int kAlign = 16;
// alignment is 16. sws in ffmpeg is 16, libav10 is 8. if not aligned sws will print warnings and go slow code paths
const int kAlign = DataAlignment;
AV_ENSURE(av_image_fill_linesizes((int*)d.pitchs.constData(), d.fmt_out, kAlign > 7 ? FFALIGN(d.w_out, 8) : d.w_out), false);
for (int i = 0; i < d.pitchs.size(); ++i)
d.pitchs[i] = FFALIGN(d.pitchs[i], kAlign);
Expand Down
5 changes: 4 additions & 1 deletion src/ImageConverter.h
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-2016 Wang Bin <[email protected]>
Copyright (C) 2012-2018 Wang Bin <[email protected]>
* This file is part of QtAV
Expand Down Expand Up @@ -35,9 +35,12 @@ class ImageConverter //export is not needed
{
DPTR_DECLARE_PRIVATE(ImageConverter)
public:
enum { DataAlignment = 16 };

ImageConverter();
virtual ~ImageConverter();

// the real data starts with DataAlignment (16bit) aligned address
QByteArray outData() const;
// return false if i/o format not supported, or size is not valid.
// TODO: use isSupported(i/o format);
Expand Down
12 changes: 11 additions & 1 deletion src/QtAV/Frame.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/******************************************************************************
QtAV: Multimedia framework based on Qt and FFmpeg
Copyright (C) 2012-2016 Wang Bin <[email protected]>
Copyright (C) 2012-2018 Wang Bin <[email protected]>
* This file is part of QtAV
Expand Down Expand Up @@ -61,7 +61,17 @@ class Q_AV_EXPORT Frame
*/
int bytesPerLine(int plane = 0) const;
// the whole frame data. may be empty unless clone() or allocate is called
// real data starts with dataAlignment() aligned address
QByteArray frameData() const;
int dataAlignment() const;
uchar* frameDataPtr(int* size = NULL) const {
const int a = dataAlignment();
uchar* p = (uchar*)frameData().constData();
const int offset = (a - ((uintptr_t)p & (a-1))) & (a-1);
if (size)
*size = frameData().size() - offset;
return p+offset;
}
// deep copy 1 plane data
QByteArray data(int plane = 0) const;
uchar* bits(int plane = 0);
Expand Down
5 changes: 3 additions & 2 deletions src/QtAV/VideoFrame.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/******************************************************************************
QtAV: Multimedia framework based on Qt and FFmpeg
Copyright (C) 2012-2016 Wang Bin <[email protected]>
Copyright (C) 2012-2018 Wang Bin <[email protected]>
* This file is part of QtAV
Expand Down Expand Up @@ -52,7 +52,8 @@ class Q_AV_EXPORT VideoFrame : public Frame
VideoFrame();
//must set planes and linesize manually if data is empty
// must set planes and linesize manually
VideoFrame(int width, int height, const VideoFormat& format, const QByteArray& data = QByteArray());
// alignment: data ptr alignment
VideoFrame(int width, int height, const VideoFormat& format, const QByteArray& data = QByteArray(), int alignment = 1);
VideoFrame(const QImage& image);
VideoFrame(const VideoFrame &other);
~VideoFrame();
Expand Down
4 changes: 3 additions & 1 deletion src/QtAV/private/Frame_p.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/******************************************************************************
QtAV: Multimedia framework based on Qt and FFmpeg
Copyright (C) 2012-2016 Wang Bin <[email protected]>
Copyright (C) 2012-2018 Wang Bin <[email protected]>
* This file is part of QtAV (from 2013)
Expand Down Expand Up @@ -36,6 +36,7 @@ class FramePrivate : public QSharedData
public:
FramePrivate()
: timestamp(0)
, data_align(1)
{}
virtual ~FramePrivate() {}

Expand All @@ -44,6 +45,7 @@ class FramePrivate : public QSharedData
QVariantMap metadata;
QByteArray data;
qreal timestamp;
int data_align;
};

} //namespace QtAV
Expand Down
6 changes: 4 additions & 2 deletions src/VideoCapture.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/******************************************************************************
VideoCapture.cpp: description
Copyright (C) 2012-2016 Wang Bin <[email protected]>
Copyright (C) 2012-2018 Wang Bin <[email protected]>
* This file is part of QtAV
Expand Down Expand Up @@ -84,7 +84,9 @@ class CaptureTask : public QRunnable
QMetaObject::invokeMethod(cap, "failed");
return;
}
if (file.write(frame.frameData()) <= 0) {
int sz = 0;
const char* data = (const char*)frame.frameDataPtr(&sz);
if (file.write(data, sz) <= 0) {
qWarning("VideoCapture is failed to write captured frame with original format");
QMetaObject::invokeMethod(cap, "failed");
file.close();
Expand Down
9 changes: 5 additions & 4 deletions src/VideoFrame.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/******************************************************************************
QtAV: Multimedia framework based on Qt and FFmpeg
Copyright (C) 2012-2016 Wang Bin <[email protected]>
Copyright (C) 2012-2018 Wang Bin <[email protected]>
* This file is part of QtAV
Expand Down Expand Up @@ -154,11 +154,12 @@ VideoFrame::VideoFrame()
{
}

VideoFrame::VideoFrame(int width, int height, const VideoFormat &format, const QByteArray& data)
VideoFrame::VideoFrame(int width, int height, const VideoFormat &format, const QByteArray& data, int alignment)
: Frame(new VideoFramePrivate(width, height, format))
{
Q_D(VideoFrame);
d->data = data;
d->data_align = alignment;
}

VideoFrame::VideoFrame(const QImage& image)
Expand Down Expand Up @@ -353,7 +354,7 @@ QImage VideoFrame::toImage(QImage::Format fmt, const QSize& dstSize, const QRect
VideoFrame f(to(VideoFormat(VideoFormat::pixelFormatFromImageFormat(fmt)), dstSize, roi));
if (!f)
return QImage();
QImage image((const uchar*)f.frameData().constData(), f.width(), f.height(), f.bytesPerLine(0), fmt);
QImage image(f.frameDataPtr(), f.width(), f.height(), f.bytesPerLine(0), fmt);
return image.copy();
}

Expand Down Expand Up @@ -395,7 +396,7 @@ VideoFrame VideoFrame::to(const VideoFormat &fmt, const QSize& dstSize, const QR
qWarning() << "VideoFrame::to error: " << format() << "=>" << fmt;
return VideoFrame();
}
VideoFrame f(w, h, fmt, conv.outData());
VideoFrame f(w, h, fmt, conv.outData(), ImageConverter::DataAlignment);
f.setBits(conv.outPlanes());
f.setBytesPerLine(conv.outLineSizes());
if (fmt.isRGB()) {
Expand Down

0 comments on commit f3ad0a5

Please sign in to comment.