]> git.sesse.net Git - mlt/blob - src/modules/decklink/common.cpp
34665f51926df7f647dac265e603e30f6ab78448
[mlt] / src / modules / decklink / common.cpp
1 /*
2  * common.cpp -- Blackmagic Design DeckLink common functions
3  * Copyright (C) 2012 Dan Dennedy <dan@dennedy.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with consumer library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include "common.h"
21 #include <stdlib.h>
22 #include <unistd.h>
23
24 #ifdef __DARWIN__
25
26 char* getCString( DLString aDLString )
27 {
28         char* CString = (char*) malloc( 64 );
29         CFStringGetCString( aDLString, CString, 64, kCFStringEncodingMacRoman );
30         return CString;
31 }
32
33 void freeCString( char* aCString )
34 {
35         if ( aCString ) free( aCString );
36 }
37
38 void freeDLString( DLString aDLString )
39 {
40         if ( aDLString ) CFRelease( aDLString );
41 }
42
43 #elif defined(WIN32)
44
45 char* getCString( DLString aDLString )
46 {
47         char* CString = NULL;
48         if ( aDLString )
49         {
50                 int size = WideCharToMultiByte( CP_UTF8, 0, aDLString, -1, NULL, 0, NULL, NULL );
51                 if (size)
52                 {
53                         CString = new char[ size ];
54                         size = WideCharToMultiByte( CP_UTF8, 0, aDLString, -1, CString, size, NULL, NULL );
55                         if ( !size )
56                         {
57                                 delete[] CString;
58                                 CString = NULL;
59                         }
60                 }
61         }
62         return CString;
63 }
64
65 void freeCString( char* aCString )
66 {
67         delete[] aCString;
68 }
69
70 void freeDLString( DLString aDLString )
71 {
72         if ( aDLString ) free( (void*) aDLString );
73 }
74
75 #else
76
77 char* getCString( DLString aDLString )
78 {
79         return aDLString? (char*) aDLString : NULL;
80 }
81
82 void freeCString( char* aCString )
83 {
84 }
85
86 void freeDLString( DLString aDLString )
87 {
88         if ( aDLString ) free( (void*) aDLString );
89 }
90
91 #endif
92
93
94 void swab2( const void *from, void *to, int n )
95 {
96 #if defined(USE_SSE)
97 #define SWAB_STEP 16
98         __asm__ volatile
99         (
100                 "loop_start:                            \n\t"
101
102                 /* load */
103                 "movdqa         0(%[from]), %%xmm0      \n\t"
104                 "add            $0x10, %[from]          \n\t"
105
106                 /* duplicate to temp registers */
107                 "movdqa         %%xmm0, %%xmm1          \n\t"
108
109                 /* shift right temp register */
110                 "psrlw          $8, %%xmm1              \n\t"
111
112                 /* shift left main register */
113                 "psllw          $8, %%xmm0              \n\t"
114
115                 /* compose them back */
116                 "por           %%xmm0, %%xmm1           \n\t"
117
118                 /* save */
119                 "movdqa         %%xmm1, 0(%[to])        \n\t"
120                 "add            $0x10, %[to]            \n\t"
121
122                 "dec            %[cnt]                  \n\t"
123                 "jnz            loop_start              \n\t"
124
125                 :
126                 : [from]"r"(from), [to]"r"(to), [cnt]"r"(n / SWAB_STEP)
127                 //: "xmm0", "xmm1"
128         );
129
130         from = (unsigned char*) from + n - (n % SWAB_STEP);
131         to = (unsigned char*) to + n - (n % SWAB_STEP);
132         n = (n % SWAB_STEP);
133 #endif
134         swab(from, to, n);
135 };