forked from wang-bin/QtAV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VideoDecoder.cpp
169 lines (146 loc) · 5.53 KB
/
VideoDecoder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/******************************************************************************
QtAV: Media play library based on Qt and FFmpeg
Copyright (C) 2012-2014 Wang Bin <[email protected]>
* This file is part of QtAV
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
******************************************************************************/
#include <QtAV/VideoDecoder.h>
#include <QtAV/private/VideoDecoder_p.h>
#include <QtCore/QSize>
#include "QtAV/private/factory.h"
namespace QtAV {
FACTORY_DEFINE(VideoDecoder)
extern void RegisterVideoDecoderFFmpeg_Man();
extern void RegisterVideoDecoderDXVA_Man();
extern void RegisterVideoDecoderCUDA_Man();
extern void RegisterVideoDecoderVAAPI_Man();
extern void RegisterVideoDecoderVDA_Man();
extern void RegisterVideoDecoderCedarv_Man();
void VideoDecoder_RegisterAll()
{
RegisterVideoDecoderFFmpeg_Man();
#if QTAV_HAVE(DXVA)
RegisterVideoDecoderDXVA_Man();
#endif //QTAV_HAVE(DXVA)
#if QTAV_HAVE(CUDA)
RegisterVideoDecoderCUDA_Man();
#endif //QTAV_HAVE(CUDA)
#if QTAV_HAVE(VAAPI)
RegisterVideoDecoderVAAPI_Man();
#endif //QTAV_HAVE(VAAPI)
#if QTAV_HAVE(VDA)
RegisterVideoDecoderVDA_Man();
#endif //QTAV_HAVE(VDA)
#if QTAV_HAVE(CEDARV)
RegisterVideoDecoderCedarv_Man();
#endif //QTAV_HAVE(CEDARV)
}
VideoDecoder::VideoDecoder()
:AVDecoder(*new VideoDecoderPrivate())
{
}
VideoDecoder::VideoDecoder(VideoDecoderPrivate &d):
AVDecoder(d)
{
}
QString VideoDecoder::name() const
{
return QString(VideoDecoderFactory::name(id()).c_str());
}
bool VideoDecoder::prepare()
{
return AVDecoder::prepare();
}
//TODO: use ipp, cuda decode and yuv functions. is sws_scale necessary?
bool VideoDecoder::decode(const QByteArray &encoded)
{
if (!isAvailable())
return false;
DPTR_D(VideoDecoder);
AVPacket packet;
#if NO_PADDING_DATA
/*!
larger than the actual read bytes because some optimized bitstream readers read 32 or 64 bits at once and could read over the end.
The end of the input buffer avpkt->data should be set to 0 to ensure that no overreading happens for damaged MPEG streams
*/
// auto released by av_buffer_default_free
av_new_packet(&packet, encoded.size());
memcpy(packet.data, encoded.data(), encoded.size());
#else
av_init_packet(&packet);
packet.size = encoded.size();
packet.data = (uint8_t*)encoded.constData();
#endif //NO_PADDING_DATA
//TODO: use AVPacket directly instead of Packet?
//AVStream *stream = format_context->streams[stream_idx];
//TODO: some decoders might in addition need other fields like flags&AV_PKT_FLAG_KEY
int ret = avcodec_decode_video2(d.codec_ctx, d.frame, &d.got_frame_ptr, &packet);
//qDebug("pic_type=%c", av_get_picture_type_char(d.frame->pict_type));
d.undecoded_size = qMin(encoded.size() - ret, encoded.size());
//TODO: decoded format is YUV420P, YUV422P?
av_free_packet(&packet);
if (ret < 0) {
qWarning("[VideoDecoder] %s", av_err2str(ret));
return false;
}
if (!d.got_frame_ptr) {
qWarning("no frame could be decompressed: %s", av_err2str(ret));
return true;
}
if (!d.codec_ctx->width || !d.codec_ctx->height)
return false;
//qDebug("codec %dx%d, frame %dx%d", d.codec_ctx->width, d.codec_ctx->height, d.frame->width, d.frame->height);
d.width = d.frame->width;
d.height = d.frame->height;
//avcodec_align_dimensions2(d.codec_ctx, &d.width_align, &d.height_align, aligns);
return true;
}
void VideoDecoder::resizeVideoFrame(const QSize &size)
{
resizeVideoFrame(size.width(), size.height());
}
/*
* width, height: the decoded frame size
* 0, 0 to reset to original video frame size
*/
void VideoDecoder::resizeVideoFrame(int width, int height)
{
DPTR_D(VideoDecoder);
d.width = width;
d.height = height;
}
int VideoDecoder::width() const
{
return d_func().width;
}
int VideoDecoder::height() const
{
return d_func().height;
}
VideoFrame VideoDecoder::frame()
{
DPTR_D(VideoDecoder);
if (d.width <= 0 || d.height <= 0 || !d.codec_ctx)
return VideoFrame(0, 0, VideoFormat(VideoFormat::Format_Invalid));
//DO NOT make frame as a memeber, because VideoFrame is explictly shared!
float displayAspectRatio = 0;
if (d.codec_ctx->sample_aspect_ratio.den > 0)
displayAspectRatio = ((float)d.frame->width / (float)d.frame->height) *
((float)d.codec_ctx->sample_aspect_ratio.num / (float)d.codec_ctx->sample_aspect_ratio.den);
VideoFrame frame(d.frame->width, d.frame->height, VideoFormat((int)d.codec_ctx->pix_fmt));
frame.setDisplayAspectRatio(displayAspectRatio);
frame.setBits(d.frame->data);
frame.setBytesPerLine(d.frame->linesize);
return frame;
}
} //namespace QtAV