35
35
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
36
*/
37
37
38
- #include < cstdio> // for printf, fprintf, stderr
38
+ #include < cstdio> // for printf, stderr
39
39
#include < cstdlib> // for free, malloc
40
40
#include < memory> // for shared_ptr
41
41
52
52
#include " video_compress.h"
53
53
#include " video_frame.h" // for vf_get_tile, video_desc_from_f...
54
54
55
+ #define MOD_NAME " [CUDA DXT] "
56
+
57
+ #define CHECK_CUDA (cmd, msg, action ) \
58
+ if ((cmd) != CUDA_WRAPPER_SUCCESS) { \
59
+ MSG (ERROR, " %s: %s\n " , msg, cuda_wrapper_last_error_string ()); \
60
+ action; \
61
+ }
62
+
63
+
55
64
using namespace std ;
56
65
57
66
namespace {
@@ -144,25 +153,22 @@ static bool configure_with(struct state_video_compress_cuda_dxt *s, struct video
144
153
codec_t supported_codecs[] = { RGB, UYVY, VIDEO_CODEC_NONE };
145
154
s->decoder = get_best_decoder_from (desc.color_spec , supported_codecs, &s->in_codec );
146
155
if (!s->decoder ) {
147
- fprintf (stderr, " Unsupported codec: %s\n " , get_codec_name (desc.color_spec ));
156
+ MSG (ERROR, " Unsupported codec: %s\n " ,
157
+ get_codec_name (desc.color_spec ));
148
158
return false ;
149
159
}
150
160
151
161
if (s->in_codec == UYVY) {
152
- if (CUDA_WRAPPER_SUCCESS != cuda_wrapper_malloc ((void **) &s->cuda_uyvy_buffer ,
153
- desc.width * desc.height * 2 )) {
154
- fprintf (stderr, " Could not allocate CUDA UYVY buffer.\n " );
155
- return false ;
156
- }
162
+ CHECK_CUDA (cuda_wrapper_malloc ((void **) &s->cuda_uyvy_buffer ,
163
+ desc.width * desc.height * 2 ),
164
+ " Could not allocate CUDA UYVY buffer" , return false );
157
165
}
158
166
159
167
s->in_buffer = (char *) malloc (desc.width * desc.height * 3 );
160
168
161
- if (CUDA_WRAPPER_SUCCESS != cuda_wrapper_malloc ((void **) &s->cuda_in_buffer ,
162
- desc.width * desc.height * 3 )) {
163
- fprintf (stderr, " Could not allocate CUDA output buffer.\n " );
164
- return false ;
165
- }
169
+ CHECK_CUDA (cuda_wrapper_malloc ((void **) &s->cuda_in_buffer ,
170
+ desc.width * desc.height * 3 ),
171
+ " Could not allocate CUDA output buffer" , return false );
166
172
167
173
struct video_desc compressed_desc = desc;
168
174
compressed_desc.color_spec = s->out_codec ;
@@ -171,12 +177,8 @@ static bool configure_with(struct state_video_compress_cuda_dxt *s, struct video
171
177
172
178
s->pool .reconfigure (compressed_desc, data_len);
173
179
174
- if (CUDA_WRAPPER_SUCCESS != cuda_wrapper_malloc ((void **)
175
- &s->cuda_out_buffer ,
176
- data_len)) {
177
- fprintf (stderr, " Could not allocate CUDA output buffer.\n " );
178
- return false ;
179
- }
180
+ CHECK_CUDA (cuda_wrapper_malloc ((void **) &s->cuda_out_buffer , data_len),
181
+ " Could not allocate CUDA output buffer" , return false );
180
182
181
183
return true ;
182
184
}
@@ -196,7 +198,7 @@ shared_ptr<video_frame> cuda_dxt_compress_tile(void *state, shared_ptr<video_fra
196
198
if (configure_with (s, video_desc_from_frame (tx.get ()))) {
197
199
s->saved_desc = video_desc_from_frame (tx.get ());
198
200
} else {
199
- fprintf (stderr , " [CUDA DXT] Reconfiguration failed!\n " );
201
+ MSG (ERROR , " Reconfiguration failed!\n " );
200
202
return NULL ;
201
203
}
202
204
}
@@ -218,24 +220,22 @@ shared_ptr<video_frame> cuda_dxt_compress_tile(void *state, shared_ptr<video_fra
218
220
}
219
221
220
222
if (s->in_codec == UYVY) {
221
- if (cuda_wrapper_memcpy (s->cuda_uyvy_buffer , in_buffer, tx->tiles [0 ].width *
222
- tx->tiles [0 ].height * 2 ,
223
- CUDA_WRAPPER_MEMCPY_HOST_TO_DEVICE) != CUDA_WRAPPER_SUCCESS) {
224
- fprintf (stderr, " Memcpy failed: %s\n " , cuda_wrapper_last_error_string ());
225
- return NULL ;
226
- }
227
- if (cuda_yuv422_to_yuv444 (s->cuda_uyvy_buffer , s->cuda_in_buffer ,
228
- tx->tiles [0 ].width *
229
- tx->tiles [0 ].height , 0 ) != CUDA_WRAPPER_SUCCESS) {
230
- fprintf (stderr, " Kernel failed: %s\n " , cuda_wrapper_last_error_string ());
231
- }
223
+ CHECK_CUDA (cuda_wrapper_memcpy (
224
+ s->cuda_uyvy_buffer , in_buffer,
225
+ tx->tiles [0 ].width * tx->tiles [0 ].height * 2 ,
226
+ CUDA_WRAPPER_MEMCPY_HOST_TO_DEVICE),
227
+ " Memcpy failed" , return nullptr );
228
+
229
+ CHECK_CUDA (cuda_yuv422_to_yuv444 (
230
+ s->cuda_uyvy_buffer , s->cuda_in_buffer ,
231
+ tx->tiles [0 ].width * tx->tiles [0 ].height , 0 ),
232
+ " Kernel failed" , return nullptr );
232
233
} else {
233
- if (cuda_wrapper_memcpy (s->cuda_in_buffer , in_buffer, tx->tiles [0 ].width *
234
- tx->tiles [0 ].height * 3 ,
235
- CUDA_WRAPPER_MEMCPY_HOST_TO_DEVICE) != CUDA_WRAPPER_SUCCESS) {
236
- fprintf (stderr, " Memcpy failed: %s\n " , cuda_wrapper_last_error_string ());
237
- return NULL ;
238
- }
234
+ CHECK_CUDA (cuda_wrapper_memcpy (
235
+ s->cuda_in_buffer , in_buffer,
236
+ tx->tiles [0 ].width * tx->tiles [0 ].height * 3 ,
237
+ CUDA_WRAPPER_MEMCPY_HOST_TO_DEVICE),
238
+ " Memcpy failed" , return nullptr );
239
239
}
240
240
241
241
int (*cuda_dxt_enc_func)(const void * src, void * out, int size_x, int size_y,
@@ -254,21 +254,16 @@ shared_ptr<video_frame> cuda_dxt_compress_tile(void *state, shared_ptr<video_fra
254
254
cuda_dxt_enc_func = cuda_yuv_to_dxt6;
255
255
}
256
256
}
257
- int ret = cuda_dxt_enc_func (s->cuda_in_buffer , s->cuda_out_buffer ,
258
- s->saved_desc .width , s->saved_desc .height , 0 );
259
- if (ret != 0 ) {
260
- fprintf (stderr, " Encoding failed: %s\n " , cuda_wrapper_last_error_string ());
261
- return NULL ;
262
- }
257
+ CHECK_CUDA (cuda_dxt_enc_func (s->cuda_in_buffer , s->cuda_out_buffer ,
258
+ s->saved_desc .width , s->saved_desc .height ,
259
+ 0 ),
260
+ " Encoding failed" , return nullptr );
263
261
264
262
shared_ptr<video_frame> out = s->pool .get_frame ();
265
- if (cuda_wrapper_memcpy (out->tiles [0 ].data ,
266
- s->cuda_out_buffer ,
267
- out->tiles [0 ].data_len ,
268
- CUDA_WRAPPER_MEMCPY_DEVICE_TO_HOST) != CUDA_WRAPPER_SUCCESS) {
269
- fprintf (stderr, " Memcpy failed: %s\n " , cuda_wrapper_last_error_string ());
270
- return NULL ;
271
- }
263
+ CHECK_CUDA (cuda_wrapper_memcpy (out->tiles [0 ].data , s->cuda_out_buffer ,
264
+ out->tiles [0 ].data_len ,
265
+ CUDA_WRAPPER_MEMCPY_DEVICE_TO_HOST),
266
+ " Memcpy failed" , return nullptr );
272
267
273
268
return out;
274
269
}
0 commit comments