Skip to content

Commit

Permalink
VideoFrame: check dest format & size in to(). remove 1 ctor
Browse files Browse the repository at this point in the history
  • Loading branch information
wang-bin committed Sep 23, 2015
1 parent bab7616 commit dc3ba1c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
8 changes: 4 additions & 4 deletions src/QtAV/VideoFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ class Q_AV_EXPORT VideoFrame : public Frame
* \param src CPU accessible address of frame planes on GPU. src[0] must be valid. src[i>0] will be filled depending on pixel format, pitch and surface_h if it's NULL.
* \param pitch plane pitch on GPU. pitch[0] must be valid. pitch[i>0] will be filled depending on pixel format, pitch[0] and surface_h if it's NULL.
* \param optimized try to use SIMD to copy from GPU. otherwise use memcpy
* \param swapUV
* \param swapUV it"s required if u/v src are null
*/
static VideoFrame fromGPU(const VideoFormat& fmt, int width, int height, int surface_h, quint8 *src[], int pitch[], bool optimized = true, bool swapUV = false);

VideoFrame();
//must set planes and linesize manually
VideoFrame(int width, int height, const VideoFormat& format);
//must set planes and linesize manually if data is empty
VideoFrame(int width, int height, const VideoFormat& format, const QByteArray& data = QByteArray());
//set planes and linesize manually or call init
VideoFrame(const QByteArray& data, int width, int height, const VideoFormat& format);
QTAV_DEPRECATED VideoFrame(const QByteArray& data, int width, int height, const VideoFormat& format);
VideoFrame(const QVector<int>& textures, int width, int height, const VideoFormat& format);
VideoFrame(const QImage& image); // does not copy the image data
VideoFrame(const VideoFrame &other);
Expand Down
24 changes: 13 additions & 11 deletions src/VideoFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ VideoFrame VideoFrame::fromGPU(const VideoFormat& fmt, int width, int height, in
gpu_memcpy(dst[i], src[i], pitch[i]*h[i]);

}
frame = VideoFrame(buf, width, height, fmt);
frame = VideoFrame(width, height, fmt, buf);
frame.setBits(dst);
frame.setBytesPerLine(pitch);
} else {
Expand Down Expand Up @@ -139,9 +139,11 @@ VideoFrame::VideoFrame()
{
}

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

VideoFrame::VideoFrame(const QByteArray& data, int width, int height, const VideoFormat &format)
Expand Down Expand Up @@ -221,7 +223,7 @@ VideoFrame VideoFrame::clone() const

QByteArray buf(bytes, 0);
char *dst = buf.data(); //must before buf is shared, otherwise data will be detached.
VideoFrame f(buf, width(), height(), d->format);
VideoFrame f(width(), height(), d->format, buf);
const int nb_planes = d->format.planeCount();
for (int i = 0; i < nb_planes; ++i) {
f.setBits((quint8*)dst, i);
Expand Down Expand Up @@ -386,24 +388,24 @@ VideoFrame VideoFrame::to(const VideoFormat &fmt, const QSize& dstSize, const QR
}
return VideoFrame();
}
if (fmt.pixelFormatFFmpeg() == pixelFormatFFmpeg())
const int w = dstSize.width() > 0 ? dstSize.width() : width();
const int h = dstSize.height() > 0 ? dstSize.height() : height();
if (fmt.pixelFormatFFmpeg() == pixelFormatFFmpeg()
&& w == width() && h == height()
// TODO: roi check.
)
return *this;
Q_D(const VideoFrame);
ImageConverterSWS conv;
conv.setInFormat(pixelFormatFFmpeg());
conv.setOutFormat(fmt.pixelFormatFFmpeg());
conv.setInSize(width(), height());
int w = width(), h = height();
if (dstSize.width() > 0)
w = dstSize.width();
if (dstSize.height() > 0)
h = dstSize.height();
conv.setOutSize(w, h);
if (!conv.convert(d->planes.constData(), d->line_sizes.constData())) {
qWarning() << "VideoFrame::to error: " << format() << "=>" << fmt;
return VideoFrame();
}
VideoFrame f(conv.outData(), w, h, fmt);
VideoFrame f(w, h, fmt, conv.outData());
f.setBits(conv.outPlanes());
f.setBytesPerLine(conv.outLineSizes());
if (fmt.isRGB()) {
Expand Down Expand Up @@ -546,7 +548,7 @@ VideoFrame VideoFrameConverter::convert(const VideoFrame &frame, int fffmt) cons
return VideoFrame();
}
const VideoFormat fmt(fffmt);
VideoFrame f(m_cvt->outData(), frame.width(), frame.height(), fmt);
VideoFrame f(frame.width(), frame.height(), fmt, m_cvt->outData());
f.setBits(m_cvt->outPlanes());
f.setBytesPerLine(m_cvt->outLineSizes());
f.setTimestamp(frame.timestamp());
Expand Down
2 changes: 1 addition & 1 deletion src/codec/video/VideoDecoderCedarv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ VideoFrame VideoDecoderCedarv::frame()
d.map_c(d.cedarPicture.u, plane[1], plane[2], pitch[1], display_w_align, display_h_align/2); //vdpau use w, h/2

const VideoFormat fmt(nv12 ? VideoFormat::Format_NV12 : VideoFormat::Format_YUV420P);
VideoFrame frame(buf, display_w_align, display_h_align, fmt);
VideoFrame frame(display_w_align, display_h_align, fmt, buf);
frame.setBits(plane);
frame.setBytesPerLine(pitch);
frame.setTimestamp(qreal(d.cedarPicture.pts)/1000.0);
Expand Down
4 changes: 2 additions & 2 deletions src/codec/video/VideoDecoderFFmpegHW.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ bool VideoDecoderFFmpegHWPrivate::prepare()
break;
}
//// From vlc end
codec_ctx->opaque = this; //is it ok?
codec_ctx->opaque = this;

pixfmt = codec_ctx->pix_fmt;
get_format = codec_ctx->get_format;
Expand Down Expand Up @@ -305,7 +305,7 @@ VideoFrame VideoDecoderFFmpegHW::copyToFrame(const VideoFormat& fmt, int surface
plane_ptr += pitch[i] * h[i];
d.gpu_mem.copyFrame(src[i], dst[i], pitch[i], h[i], pitch[i]);
}
frame = VideoFrame(buf, width(), height(), fmt);
frame = VideoFrame(width(), height(), fmt, buf);
frame.setBits(dst);
frame.setBytesPerLine(pitch);
} else {
Expand Down

0 comments on commit dc3ba1c

Please sign in to comment.