@@ -45,7 +45,7 @@ UNIFEX_TERM get_jpeg_header(UnifexEnv* env, UnifexPayload *payload) {
45
45
enum TJSAMP tjsamp ;
46
46
enum TJCS cspace ;
47
47
int res , width , height ;
48
- ERL_NIF_TERM map_out , ret ;
48
+ UNIFEX_TERM ret ;
49
49
50
50
tjh = tjInitDecompress ();
51
51
if (!tjh )
@@ -63,45 +63,24 @@ UNIFEX_TERM get_jpeg_header(UnifexEnv* env, UnifexPayload *payload) {
63
63
goto cleanup ;
64
64
}
65
65
66
- // unifex does not support maps yes.
67
- // See https://github.com/membraneframework/unifex/issues/32
68
- if (!enif_make_map_from_arrays (
69
- env ,
70
- (ERL_NIF_TERM []) {
71
- enif_make_atom (env , "width" ),
72
- enif_make_atom (env , "height" ),
73
- enif_make_atom (env , "format" ),
74
-
75
- },
76
- (ERL_NIF_TERM []) {
77
- enif_make_int (env , width ),
78
- enif_make_int (env , height ),
79
- enif_make_atom (env , tjsamp_to_format (tjsamp ))
80
- },
81
- 3 , & map_out
82
- )) {
83
- ret = get_jpeg_header_result_error (env , "make_map" );
84
- goto cleanup ;
85
- } else {
86
- // Generated code does not support maps currently.
87
- ret = enif_make_tuple_from_array (env , (ERL_NIF_TERM []) {enif_make_atom (env , "ok" ), map_out }, 2 );
88
- goto cleanup ;
89
- }
66
+ jpeg_header header = {(char * )tjsamp_to_format (tjsamp ), width , height };
67
+ ret = get_jpeg_header_result_ok (env , header );
68
+
90
69
cleanup :
91
70
if (tjh ) tjDestroy (tjh );
92
71
return ret ;
93
72
}
94
73
95
74
/**
96
- * Convert a binary h264 payload into a jpeg encoded payload
75
+ * Convert a yuv binary payload into a jpeg encoded payload
97
76
*/
98
77
UNIFEX_TERM yuv_to_jpeg (UnifexEnv * env , UnifexPayload * payload , int width , int height , int quality , char * format ) {
99
78
tjhandle tjh = NULL ;
100
79
enum TJSAMP tjsamp ;
101
80
unsigned char * jpegBuf = NULL ;
102
81
unsigned long jpegSize ;
103
82
int res ;
104
- UnifexPayload * jpegFrame ;
83
+ UnifexPayload * jpegFrame = NULL ;
105
84
UNIFEX_TERM ret ;
106
85
107
86
res = format_to_tjsamp (format );
@@ -126,18 +105,19 @@ UNIFEX_TERM yuv_to_jpeg(UnifexEnv* env, UnifexPayload *payload, int width, int h
126
105
if (res < 0 ) {
127
106
ret = yuv_to_jpeg_result_error (env , tjGetErrorStr2 (tjh ));
128
107
goto cleanup ;
129
- } else {
130
- jpegFrame = unifex_payload_alloc (env , UNIFEX_PAYLOAD_BINARY , jpegSize );
131
- if (!jpegFrame ) {
132
- ret = yuv_to_jpeg_result_error (env , "payload_alloc" );
133
- } else {
134
- memcpy (jpegFrame -> data , jpegBuf , jpegSize );
135
- ret = yuv_to_jpeg_result_ok (env , jpegFrame );
136
- }
137
- goto cleanup ;
138
108
}
139
109
110
+ jpegFrame = unifex_alloc (sizeof (* jpegFrame ));
111
+ unifex_payload_alloc (env , UNIFEX_PAYLOAD_BINARY , jpegSize , jpegFrame );
112
+ memcpy (jpegFrame -> data , jpegBuf , jpegSize );
113
+ ret = yuv_to_jpeg_result_ok (env , jpegFrame );
114
+
140
115
cleanup :
116
+ if (jpegFrame != NULL ) {
117
+ unifex_payload_release (jpegFrame );
118
+ unifex_free (jpegFrame );
119
+ }
120
+
141
121
if (jpegBuf ) tjFree (jpegBuf );
142
122
if (tjh ) tjDestroy (tjh );
143
123
return ret ;
@@ -151,7 +131,7 @@ UNIFEX_TERM jpeg_to_yuv(UnifexEnv* env, UnifexPayload *payload) {
151
131
enum TJSAMP tjsamp ;
152
132
enum TJCS cspace ;
153
133
unsigned long yuvBufSize ;
154
- UnifexPayload * yuvFrame ;
134
+ UnifexPayload * yuvFrame = NULL ;
155
135
int res , width , height ;
156
136
UNIFEX_TERM ret ;
157
137
@@ -166,17 +146,15 @@ UNIFEX_TERM jpeg_to_yuv(UnifexEnv* env, UnifexPayload *payload) {
166
146
& width , & height ,
167
147
(int * )& tjsamp , (int * )& cspace
168
148
);
149
+
169
150
if (res < 0 ) {
170
151
ret = jpeg_to_yuv_result_error (env , tjGetErrorStr2 (tjh ));
171
152
goto cleanup ;
172
153
}
173
154
174
155
yuvBufSize = tjBufSizeYUV2 (width , 4 , height , tjsamp );
175
- yuvFrame = unifex_payload_alloc (env , UNIFEX_PAYLOAD_BINARY , yuvBufSize );
176
- if (!yuvFrame ) {
177
- ret = jpeg_to_yuv_result_error (env , "could not allocate frame" );
178
- goto cleanup ;
179
- }
156
+ yuvFrame = unifex_alloc (sizeof (* yuvFrame ));
157
+ unifex_payload_alloc (env , UNIFEX_PAYLOAD_BINARY , yuvBufSize , yuvFrame );
180
158
181
159
res = tjDecompressToYUV2 (
182
160
tjh ,
@@ -190,12 +168,16 @@ UNIFEX_TERM jpeg_to_yuv(UnifexEnv* env, UnifexPayload *payload) {
190
168
if (res < 0 ) {
191
169
ret = jpeg_to_yuv_result_error (env , tjGetErrorStr2 (tjh ));
192
170
goto cleanup ;
193
- } else {
194
- ret = jpeg_to_yuv_result_ok (env , yuvFrame );
195
- goto cleanup ;
196
171
}
172
+
173
+ ret = jpeg_to_yuv_result_ok (env , yuvFrame );
197
174
198
175
cleanup :
176
+ if (yuvFrame != NULL ) {
177
+ unifex_payload_release (yuvFrame );
178
+ unifex_free (yuvFrame );
179
+ }
180
+
199
181
if (tjh ) tjDestroy (tjh );
200
182
return ret ;
201
183
}
0 commit comments