-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathUncompressedVideoSampleProvider.cpp
More file actions
executable file
·137 lines (120 loc) · 3.46 KB
/
Copy pathUncompressedVideoSampleProvider.cpp
File metadata and controls
executable file
·137 lines (120 loc) · 3.46 KB
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
//*****************************************************************************
//
// Copyright 2015 Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http ://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//*****************************************************************************
#include "pch.h"
#include "UncompressedVideoSampleProvider.h"
extern "C"
{
#include <libavutil/imgutils.h>
}
using namespace FFmpegInterop;
UncompressedVideoSampleProvider::UncompressedVideoSampleProvider(
FFmpegReader^ reader,
AVFormatContext* avFormatCtx,
AVCodecContext* avCodecCtx)
: MediaSampleProvider(reader, avFormatCtx, avCodecCtx)
, m_pAvFrame(nullptr)
, m_pSwsCtx(nullptr)
{
for (int i = 0; i < 4; i++)
{
m_rgVideoBufferLineSize[i] = 0;
m_rgVideoBufferData[i] = nullptr;
}
}
HRESULT UncompressedVideoSampleProvider::AllocateResources()
{
HRESULT hr = S_OK;
hr = MediaSampleProvider::AllocateResources();
if (SUCCEEDED(hr))
{
// Setup software scaler to convert any decoder pixel format (e.g. YUV420P) to NV12 that is supported in Windows & Windows Phone MediaElement
m_pSwsCtx = sws_getContext(
m_pAvCodecCtx->width,
m_pAvCodecCtx->height,
m_pAvCodecCtx->pix_fmt,
m_pAvCodecCtx->width,
m_pAvCodecCtx->height,
AV_PIX_FMT_NV12,
SWS_BICUBIC,
NULL,
NULL,
NULL);
if (m_pSwsCtx == nullptr)
{
hr = E_OUTOFMEMORY;
}
}
if (SUCCEEDED(hr))
{
m_pAvFrame = av_frame_alloc();
if (m_pAvFrame == nullptr)
{
hr = E_OUTOFMEMORY;
}
}
if (SUCCEEDED(hr))
{
if (av_image_alloc(m_rgVideoBufferData, m_rgVideoBufferLineSize, m_pAvCodecCtx->width, m_pAvCodecCtx->height, AV_PIX_FMT_NV12, 1) < 0)
{
hr = E_FAIL;
}
}
return hr;
}
UncompressedVideoSampleProvider::~UncompressedVideoSampleProvider()
{
if (m_pAvFrame)
{
av_freep(m_pAvFrame);
}
if (m_rgVideoBufferData)
{
av_freep(m_rgVideoBufferData);
}
}
HRESULT UncompressedVideoSampleProvider::WriteAVPacketToStream(DataWriter^ dataWriter, AVPacket* avPacket)
{
// Convert decoded video pixel format to NV12 using FFmpeg software scaler
if (sws_scale(m_pSwsCtx, (const uint8_t **)(m_pAvFrame->data), m_pAvFrame->linesize, 0, m_pAvCodecCtx->height, m_rgVideoBufferData, m_rgVideoBufferLineSize) < 0)
{
return E_FAIL;
}
auto YBuffer = ref new Platform::Array<uint8_t>(m_rgVideoBufferData[0], m_rgVideoBufferLineSize[0] * m_pAvCodecCtx->height);
auto UVBuffer = ref new Platform::Array<uint8_t>(m_rgVideoBufferData[1], m_rgVideoBufferLineSize[1] * m_pAvCodecCtx->height / 2);
dataWriter->WriteBytes(YBuffer);
dataWriter->WriteBytes(UVBuffer);
av_frame_unref(m_pAvFrame);
return S_OK;
}
HRESULT UncompressedVideoSampleProvider::DecodeAVPacket(DataWriter^ dataWriter, AVPacket* avPacket)
{
int frameComplete = 0;
if (avcodec_decode_video2(m_pAvCodecCtx, m_pAvFrame, &frameComplete, avPacket) < 0)
{
DebugMessage(L"DecodeAVPacket Failed\n");
frameComplete = 1;
}
else
{
if (frameComplete)
{
avPacket->pts = av_frame_get_best_effort_timestamp(m_pAvFrame);
}
}
return frameComplete ? S_OK : S_FALSE;
}