My Project
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules
mvd.c
Go to the documentation of this file.
1 /*
2  mvd.c - code for using this: http://3dbrew.org/wiki/MVD_Services
3 */
4 
5 #include <stdlib.h>
6 #include <string.h>
7 #include <3ds.h>
8 
10 static u32 mvdstdInitialized = 0;
11 static mvdstdMode mvdstd_mode;
12 static mvdstdTypeInput mvdstd_input_type;
13 static mvdstdTypeOutput mvdstd_output_type;
14 static u32 *mvdstd_workbuf = NULL;
15 static size_t mvdstd_workbufsize = 0;
16 
17 static Result mvdstdipc_Initialize(u32 *buf, u32 bufsize, Handle kprocess)
18 {
19  u32* cmdbuf = getThreadCommandBuffer();
20  cmdbuf[0] = 0x00010082; //request header code
21  cmdbuf[1] = (u32)buf;
22  cmdbuf[2] = bufsize;
23  cmdbuf[3] = 0;
24  cmdbuf[4] = kprocess;
25 
26  Result ret=0;
27  if((ret=svcSendSyncRequest(mvdstdHandle)))return ret;
28 
29  return cmdbuf[1];
30 }
31 
32 static Result mvdstdipc_Shutdown()
33 {
34  u32* cmdbuf = getThreadCommandBuffer();
35  cmdbuf[0] = 0x00020000; //request header code
36 
37  Result ret=0;
38  if((ret = svcSendSyncRequest(mvdstdHandle)))return ret;
39 
40  return cmdbuf[1];
41 }
42 
43 static Result mvdstdipc_cmd18()
44 {
45  u32* cmdbuf = getThreadCommandBuffer();
46  cmdbuf[0] = 0x00180000; //request header code
47 
48  Result ret=0;
49  if((ret=svcSendSyncRequest(mvdstdHandle)))return ret;
50 
51  return cmdbuf[1];
52 }
53 
54 static Result mvdstdipc_cmd19()
55 {
56  u32* cmdbuf = getThreadCommandBuffer();
57  cmdbuf[0] = 0x00190000; //request header code
58 
59  Result ret=0;
60  if((ret=svcSendSyncRequest(mvdstdHandle)))return ret;
61 
62  return cmdbuf[1];
63 }
64 
65 static Result mvdstdipc_cmd1a()
66 {
67  u32* cmdbuf = getThreadCommandBuffer();
68  cmdbuf[0] = 0x001A0000; //request header code
69 
70  Result ret=0;
71  if((ret=svcSendSyncRequest(mvdstdHandle)))return ret;
72 
73  return cmdbuf[1];
74 }
75 
77 {
78  u32* cmdbuf = getThreadCommandBuffer();
79  cmdbuf[0] = 0x001E0044; //request header code
80  cmdbuf[1] = sizeof(mvdstdConfig);
81  cmdbuf[2] = 0;
82  cmdbuf[3] = 0xffff8001;
83  cmdbuf[4] = (sizeof(mvdstdConfig)<<4) | 10;
84  cmdbuf[5] = (u32)config;
85 
86  Result ret=0;
87  if((ret=svcSendSyncRequest(mvdstdHandle)))return ret;
88 
89  return cmdbuf[1];
90 }
91 
92 void mvdstdGenerateDefaultConfig(mvdstdConfig *config, u32 input_width, u32 input_height, u32 output_width, u32 output_height, u32 *vaddr_colorconv_indata, u32 *vaddr_outdata0, u32 *vaddr_outdata1_colorconv)
93 {
94  memset(config, 0, sizeof(mvdstdConfig));
95 
96  config->input_type = mvdstd_input_type;
97 
98  config->inwidth = input_width;
99  config->inheight = input_height;
100 
101  if(mvdstd_mode==MVDMODE_COLORFORMATCONV)config->physaddr_colorconv_indata = osConvertVirtToPhys((u32)vaddr_colorconv_indata);
102 
103  if(mvdstd_mode==MVDMODE_VIDEOPROCESSING)
104  {
105  config->flag_x40 = 1;
106  config->outheight0 = output_height;
107  config->outwidth0 = output_width;
108  }
109 
110  config->output_type = mvdstd_output_type;
111 
112  config->outwidth1 = output_width;
113  config->outheight1 = output_height;
114 
115  config->physaddr_outdata0 = osConvertVirtToPhys((u32)vaddr_outdata0);
116  if(mvdstd_mode==MVDMODE_COLORFORMATCONV)config->physaddr_outdata1_colorconv = osConvertVirtToPhys((u32)vaddr_outdata1_colorconv);
117 
118  config->unk_x6c[0] = 0x1;
119  config->unk_x6c[(0x84-0x6c)>>2] = 0x12a;
120  config->unk_x6c[(0x88-0x6c)>>2] = 0x199;
121  config->unk_x6c[(0x8c-0x6c)>>2] = 0xd0;
122  config->unk_x6c[(0x90-0x6c)>>2] = 0x64;
123  config->unk_x6c[(0x94-0x6c)>>2] = 0x204;
124  config->unk_x6c[(0xa8-0x6c)>>2] = 0x1;
125  config->unk_x6c[(0x104-0x6c)>>2] = 0x1;
126  config->unk_x6c[(0x110-0x6c)>>2] = 0x200;
127  config->unk_x6c[(0x114-0x6c)>>2] = 0x100;
128 }
129 
130 Result mvdstdInit(mvdstdMode mode, mvdstdTypeInput input_type, mvdstdTypeOutput output_type, u32 size)
131 {
132  Result ret=0;
133 
134  if(mvdstdInitialized)return 0;
135 
136  mvdstd_workbufsize = size;
137  mvdstd_mode = mode;
138  mvdstd_input_type = input_type;
139  mvdstd_output_type = output_type;
140 
141  if(mvdstd_mode==MVDMODE_COLORFORMATCONV)mvdstd_workbufsize = 1;
142  if(mvdstd_mode!=MVDMODE_COLORFORMATCONV)return -2;//Video processing / H.264 isn't supported atm.
143 
144  if((ret=srvGetServiceHandle(&mvdstdHandle, "mvd:STD")))return ret;
145 
146  mvdstd_workbuf = linearAlloc(mvdstd_workbufsize);
147  if(mvdstd_workbuf==NULL)return -1;
148 
149  ret = mvdstdipc_Initialize((u32*)osConvertOldLINEARMemToNew((u32)mvdstd_workbuf), mvdstd_workbufsize, 0xffff8001);
150  if(ret<0)
151  {
153  linearFree(mvdstd_workbuf);
154  return ret;
155  }
156 
157  ret = mvdstdipc_cmd18();
158  if(ret<0)
159  {
160  mvdstdipc_Shutdown();
162  linearFree(mvdstd_workbuf);
163  return ret;
164  }
165 
166  mvdstdInitialized = 1;
167 
168  return 0;
169 }
170 
172 {
173  if(!mvdstdInitialized)return 0;
174 
175  if(mvdstd_mode==MVDMODE_COLORFORMATCONV)
176  {
177  mvdstdipc_cmd19();
178  }
179 
180  mvdstdipc_Shutdown();
181 
183 
184  linearFree(mvdstd_workbuf);
185 
186  return 0;
187 }
188 
189 Result mvdstdProcessFrame(mvdstdConfig *config, u32 *h264_vaddr_inframe, u32 h264_inframesize, u32 h264_frameid)
190 {
191  Result ret;
192 
193  if(!mvdstdInitialized)return 0;
194  if(config==NULL)return -1;
195  if(mvdstd_mode!=MVDMODE_COLORFORMATCONV)return -2;
196 
197  ret = mvdstdSetConfig(config);
198  if(ret<0)return ret;
199 
200  return mvdstdipc_cmd1a();
201 }
202 
s32 Result
Definition: types.h:42
s32 svcCloseHandle(Handle handle)
Result srvGetServiceHandle(Handle *out, const char *name)
Definition: srv.c:109
u32 physaddr_outdata1_colorconv
Definition: mvd.h:34
mvdstdMode
Definition: mvd.h:5
u32 Handle
Definition: types.h:41
u32 osConvertVirtToPhys(u32 vaddr)
Definition: os.c:19
u32 outheight0
Definition: mvd.h:29
u32 outwidth0
Definition: mvd.h:29
u32 flag_x40
Definition: mvd.h:26
Handle mvdstdHandle
Definition: mvd.c:9
u32 * getThreadCommandBuffer(void)
mvdstdTypeOutput output_type
Definition: mvd.h:31
uint32_t u32
Definition: types.h:23
mvdstdTypeInput input_type
Definition: mvd.h:20
Result mvdstdSetConfig(mvdstdConfig *config)
Definition: mvd.c:76
mvdstdTypeInput
Definition: mvd.h:10
Result mvdstdShutdown()
Definition: mvd.c:171
Result mvdstdProcessFrame(mvdstdConfig *config, u32 *h264_vaddr_inframe, u32 h264_inframesize, u32 h264_frameid)
Definition: mvd.c:189
u32 inwidth
Definition: mvd.h:23
u32 outwidth1
Definition: mvd.h:32
u32 osConvertOldLINEARMemToNew(u32 addr)
Definition: os.c:32
mvdstdTypeOutput
Definition: mvd.h:15
void * linearAlloc(size_t size)
Definition: linear.cpp:102
void mvdstdGenerateDefaultConfig(mvdstdConfig *config, u32 input_width, u32 input_height, u32 output_width, u32 output_height, u32 *vaddr_colorconv_indata, u32 *vaddr_outdata0, u32 *vaddr_outdata1_colorconv)
Definition: mvd.c:92
Result mvdstdInit(mvdstdMode mode, mvdstdTypeInput input_type, mvdstdTypeOutput output_type, u32 size)
Definition: mvd.c:130
u32 physaddr_colorconv_indata
Definition: mvd.h:24
u32 outheight1
Definition: mvd.h:32
s32 svcSendSyncRequest(Handle session)
void linearFree(void *mem)
Definition: linear.cpp:113
u32 physaddr_outdata0
Definition: mvd.h:33
u32 unk_x6c[0xb0 >>2]
Definition: mvd.h:35
u32 inheight
Definition: mvd.h:23