Repository navigation
Expand file tree
/
Copy pathopenmax-encoder.c
More file actions
181 lines (157 loc) · 6.75 KB
/
Copy pathopenmax-encoder.c
File metadata and controls
181 lines (157 loc) · 6.75 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
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
170
171
172
173
174
175
176
177
178
179
180
181
/******************************************************************************
Copyright (C) 2015 by Sean Nelson <audiohacked@gmail.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include "openmax.h"
#define VIDEO_WIDTH obs_encoder_get_width(omxil->encoder)
#define VIDEO_HEIGHT obs_encoder_get_height(omxil->encoder)
#define VIDEO_FRAMERATE 60
#define VIDEO_BITRATE 50000
#define STRING_ENCODER obs_data_get_string(settings, "component_name")
const char *openmax_get_name(void)
{
return "OpenMAX IL";
}
void *openmax_create(obs_data_t *settings, obs_encoder_t *encoder)
{
struct obs_openmax *omxil = bzalloc(sizeof(struct obs_openmax));
omxil->encoder = encoder;
OMX_ERRORTYPE r;
UNUSED_PARAMETER(settings);
/* Initialize OpenMAX IL */
debug("init omx");
if ((r = OMX_Init()) != OMX_ErrorNone) {
error("OMX initialization failed!");
return NULL;
}
/* Initialize Component */
debug("init component");
if ((r = OMX_GetHandle(omxil->omx_component, (OMX_STRING)STRING_ENCODER, NULL, NULL)) != OMX_ErrorNone) {
error("Component initialization failed!");
return NULL;
}
/* Setup ports */
/* setup input buffer */
debug("setup input buffer");
OMX_PARAM_PORTDEFINITIONTYPE input;
OMX_INIT_STRUCTURE(input);
input.nPortIndex = 200;
if((r = OMX_GetParameter(omxil->omx_component, OMX_IndexParamPortDefinition, &input)) != OMX_ErrorNone) {
error("Failed to get port definition for encoder input port 200");
}
input.format.video.nFrameWidth = VIDEO_WIDTH;
input.format.video.nFrameHeight = VIDEO_HEIGHT;
input.format.video.xFramerate = VIDEO_FRAMERATE << 16;
input.format.video.nStride = (input.format.video.nFrameWidth + input.nBufferAlignment - 1) & (~(input.nBufferAlignment - 1));
input.format.video.eColorFormat = OMX_COLOR_FormatYUV420PackedPlanar;
/* setup output buffer */
debug("setup output buffer");
OMX_PARAM_PORTDEFINITIONTYPE output;
OMX_INIT_STRUCTURE(output);
output.nPortIndex = 200;
if((r = OMX_GetParameter(omxil->omx_component, OMX_IndexParamPortDefinition, &output)) != OMX_ErrorNone) {
error("Failed to get port definition for encoder input port 200");
}
output.nPortIndex = 201;
output.format.video.eColorFormat = OMX_COLOR_FormatUnused;
output.format.video.eCompressionFormat = OMX_VIDEO_CodingAVC;
// Which one is effective, this or the configuration just below?
output.format.video.nBitrate = VIDEO_BITRATE;
if((r = OMX_SetParameter(omxil->omx_component, OMX_IndexParamPortDefinition, &output)) != OMX_ErrorNone) {
error("Failed to set port definition for encoder output port 201");
}
/* configure bitrate */
OMX_VIDEO_PARAM_BITRATETYPE bitrate;
OMX_INIT_STRUCTURE(bitrate);
bitrate.eControlRate = OMX_Video_ControlRateVariable;
bitrate.nTargetBitrate = output.format.video.nBitrate;
bitrate.nPortIndex = 201;
if((r = OMX_SetParameter(omxil->omx_component, OMX_IndexParamVideoBitrate, &bitrate)) != OMX_ErrorNone) {
error("Failed to set bitrate for encoder output port 201");
}
/* configure image format */
OMX_VIDEO_PARAM_PORTFORMATTYPE format;
OMX_INIT_STRUCTURE(format);
format.nPortIndex = 201;
format.eCompressionFormat = OMX_VIDEO_CodingAVC;
if((r = OMX_SetParameter(omxil->omx_component, OMX_IndexParamVideoPortFormat, &format)) != OMX_ErrorNone) {
error("Failed to set video format for encoder output port 201");
}
/* switch component to idle state */
debug("switch component to idle");
if((r = OMX_SendCommand(omxil->omx_component, OMX_CommandStateSet, OMX_StateIdle, NULL)) != OMX_ErrorNone) {
error("Failed to switch state of the encoder component to idle");
}
block_until_state_changed(omxil->omx_component, OMX_StateIdle);
/* enable ports */
/* enable input port */
debug("enable input port");
if((r = OMX_SendCommand(omxil->omx_component, OMX_CommandPortEnable, 200, NULL)) != OMX_ErrorNone) {
error("Failed to enable encoder input port 200");
}
block_until_port_changed(omxil, omxil->omx_component, 200, OMX_TRUE);
/* enable output port */
debug("enable output port");
if((r = OMX_SendCommand(omxil->omx_component, OMX_CommandPortEnable, 201, NULL)) != OMX_ErrorNone) {
error("Failed to enable encoder output port 201");
}
block_until_port_changed(omxil, omxil->omx_component, 201, OMX_TRUE);
/* allocate input/output buffers */
/* allocate input buffer */
debug("allocate input buffer");
OMX_INIT_STRUCTURE(input);
input.nPortIndex = 200;
if((r = OMX_GetParameter(omxil->omx_component, OMX_IndexParamPortDefinition, &input)) != OMX_ErrorNone) {
error("Failed to get port definition for encoder input port 200");
}
if((r = OMX_AllocateBuffer(omxil->omx_component, &omxil->encoder_ppBuffer_in, 200, NULL, input.nBufferSize)) != OMX_ErrorNone) {
error("Failed to allocate buffer for encoder input port 200");
}
/* allocate output buffer */
debug("allocate output buffer");
OMX_INIT_STRUCTURE(output);
output.nPortIndex = 201;
if((r = OMX_GetParameter(omxil->omx_component, OMX_IndexParamPortDefinition, &output)) != OMX_ErrorNone) {
error("Failed to get port definition for encoder output port 201");
}
if((r = OMX_AllocateBuffer(omxil->omx_component, &omxil->encoder_ppBuffer_out, 201, NULL, output.nBufferSize)) != OMX_ErrorNone) {
error("Failed to allocate buffer for encoder output port 201");
}
/* switch component to executing state */
debug("switch component to executing state");
if((r = OMX_SendCommand(omxil->omx_component, OMX_CommandStateSet, OMX_StateExecuting, NULL)) != OMX_ErrorNone) {
error("Failed to switch state of the encoder component to executing");
}
block_until_state_changed(omxil->omx_component, OMX_StateExecuting);
return omxil;
}
void openmax_destroy(void *data)
{
UNUSED_PARAMETER(data);
//struct obs_openmax *omxil = data;
/* Flush buffers */
/* Disable Ports */
/* Free the buffers */
/* switch component to idle then to loaded */
/* Free the component handle */
/* Free remaining OBS structures */
}
bool openmax_encode(void *data, struct encoder_frame *frame,
struct encoder_packet *packet, bool *received_packet)
{
UNUSED_PARAMETER(data);
UNUSED_PARAMETER(frame);
UNUSED_PARAMETER(packet);
//struct obs_openmax *omxil = data;
*received_packet = false;
return false;
}