c90cabba1dbe735599611ec21fa0df5ba0aef724
1 /*
2 * Copyright (C) 2012 Texas Instruments
3 * Author: Ramprasad <x0038811@ti.com>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along with
15 * this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <fcntl.h>
20 #include <libdce.h>
21 #include <xf86drm.h>
22 #include <omap_drm.h>
23 #include <omap_drmif.h>
25 #include "util.h"
27 #define MAX_IO_BUFS 8
28 /* Used for mpeg4 esds data copy */
30 /* omap drm device handle */
31 struct omap_device *dev = NULL;
33 struct decoder {
34 struct display *disp;
35 struct buffer *framebuf;
36 Engine_Handle engine;
37 VIDDEC2_Handle codec;
38 VIDDEC2_Params *params;
39 VIDDEC2_DynamicParams *dynParams;
40 VIDDEC2_Status *status;
41 XDM1_BufDesc *inBufs;
42 XDM_BufDesc *outBufs;
43 VIDDEC2_InArgs *inArgs;
44 VIDDEC2_OutArgs *outArgs;
45 XDAS_Int8 **outBuffer;
46 XDAS_Int32 *outBufSizes;
47 char *input;
48 struct omap_bo *input_bo;
49 int input_sz, uv_offset;
50 int padded_width;
51 int padded_height;
52 int num_outBuf;
54 };
56 static void
57 usage(char *name)
58 {
59 MSG("Usage: %s -s <connector_id>:<mode> INFILE -w width -h height", name);
60 MSG("example: %s -s 4:800x480 file.yuv -w 176 -h 144", name);
61 MSG("Test of universalCopy .");
62 MSG("");
63 MSG("yuvcopytest options:");
64 MSG("\t --help: Print this help and exit.");
65 MSG("");
66 disp_usage();
67 }
69 static void
70 decoder_close(struct decoder *decoder)
71 {
72 if(!decoder) goto bailout;
73 /* free output buffers allocated by display */
74 disp_free_buffers(decoder->disp,decoder->num_outBuf);
76 if (decoder->params) dsp_dce_free(decoder->params);
77 if (decoder->dynParams) dsp_dce_free(decoder->dynParams);
78 if (decoder->status) dsp_dce_free(decoder->status);
79 if (decoder->inBufs)
80 {
81 close(decoder->inBufs->descs[0].buf);
82 dsp_dce_free(decoder->inBufs);
83 }
84 if (decoder->outBufs) dsp_dce_free(decoder->outBufs);
85 if (decoder->inArgs) dsp_dce_free(decoder->inArgs);
86 if (decoder->outArgs) dsp_dce_free(decoder->outArgs);
87 if (decoder->outBuffer) dsp_dce_free(decoder->outBuffer);
88 if (decoder->outBufSizes) dsp_dce_free(decoder->outBufSizes);
89 if (decoder->input_bo) omap_bo_del(decoder->input_bo);
90 if (decoder->codec) VIDDEC2_delete(decoder->codec);
91 if (decoder->engine) Engine_close(decoder->engine);
92 if (decoder->disp) disp_close(decoder->disp);
93 free(decoder);
94 bailout:
95 if (dev) dce_deinit(dev);
97 }
99 static struct decoder *
100 decoder_open(int argc, char** argv)
101 {
102 struct decoder *decoder;
103 char *infile = NULL;
104 int i;
105 int width , height ,padded_width, padded_height;
106 Engine_Error ec;
107 XDAS_Int32 err;
109 decoder = calloc(1, sizeof(*decoder));
110 if (!decoder)
111 return NULL;
113 MSG("%p: Opening Display..", decoder);
114 decoder->disp = disp_open(argc, argv);
115 if (!decoder->disp)
116 goto usage;
118 /* loop thru args, find input file.. */
119 for (i = 1; i < argc; i++) {
120 int fd;
121 if (!argv[i]) {
122 continue;
123 }
124 fd = open(argv[i], 0);
125 if (fd > 0) {
126 infile = argv[i];
127 argv[i] = NULL;
128 close(fd);
129 break;
130 }
131 break;
132 }
133 for(i = 1; i < argc; i++){
134 if(!argv[i])
135 continue;
137 if(!strcmp(argv[i],"-w")){
138 argv[i] = NULL;
139 width = atoi(argv[i+1]);
140 argv[i+1] = NULL;
141 continue;
142 }
143 if(!strcmp(argv[i], "-h")){
144 argv[i] = NULL;
145 height = atoi(argv[i+1]);
146 argv[i+1] = NULL;
147 }
148 }
150 MSG("width = %d, height = %d\n", width, height);
151 if (check_args(argc, argv) || !infile)
152 goto usage;
154 /* calculate output buffer parameters: */
155 width = ALIGN2 (width, 4); /* round up to macroblocks */
156 height = ALIGN2 (height, 4); /* round up to macroblocks */
158 padded_width = width;
159 padded_height= height;
161 decoder->num_outBuf = 2;
162 decoder->padded_width = padded_width;
163 decoder->padded_height = padded_height;
165 decoder->input_sz = width * height;
166 decoder->input_bo = omap_bo_new(decoder->disp->dev,
167 decoder->input_sz, OMAP_BO_WC);
168 decoder->input = omap_bo_map(decoder->input_bo);
169 decoder->framebuf = disp_get_fb(decoder->disp);
171 MSG("Fill input buffer with yuv luma data\n");
172 {
173 FILE *fd;
174 fd = fopen(infile, "rb");
175 fread(decoder->input, width*height, sizeof(char), fd);
176 fclose(fd);
177 }
179 if (! disp_get_vid_buffers(decoder->disp, decoder->num_outBuf,
180 FOURCC_STR("NV12"), padded_width, padded_height)) {
181 ERROR("%p: could not allocate buffers", decoder);
182 goto fail;
183 }
185 MSG("%p: Opening Engine..", decoder);
186 dce_set_fd(decoder->disp->fd);
187 dev = dce_init();
188 if(dev == NULL) {
189 ERROR("%p: dce init failed", dev);
190 goto fail;
191 }
192 decoder->engine = Engine_open("dsp_vidsvr", NULL, &ec);
193 if (!decoder->engine) {
194 ERROR("%p: could not open engine", decoder);
195 goto fail;
196 }
198 decoder->params = dsp_dce_alloc(sizeof(IVIDDEC2_Params));
199 decoder->params->size = sizeof(IVIDDEC2_Params);
201 decoder->codec = VIDDEC2_create(decoder->engine,
202 "dsp_universalCopy", decoder->params);
203 if (!decoder->codec) {
204 ERROR("%p: could not create codec", decoder);
205 goto fail;
206 }
208 decoder->dynParams = dsp_dce_alloc(sizeof(IVIDDEC2_DynamicParams));
209 decoder->dynParams->size = sizeof(IVIDDEC2_DynamicParams);
211 decoder->status = dsp_dce_alloc(sizeof(IVIDDEC2_Status));
212 decoder->status->size = sizeof(IVIDDEC2_Status);
214 err = VIDDEC2_control(decoder->codec, XDM_SETPARAMS,
215 decoder->dynParams, decoder->status);
216 if (err) {
217 ERROR("%p: fail: %d", decoder, err);
218 goto fail;
219 }
221 /* not entirely sure why we need to call this here.. just copying omx.. */
222 err = VIDDEC2_control(decoder->codec, XDM_GETBUFINFO,
223 decoder->dynParams, decoder->status);
224 if (err) {
225 ERROR("%p: fail: %d", decoder, err);
226 goto fail;
227 }
229 decoder->outBuffer = dsp_dce_alloc(sizeof(XDAS_Int8*) * MAX_IO_BUFS);
230 decoder->outBufSizes = dsp_dce_alloc(sizeof(XDAS_Int32)* MAX_IO_BUFS);
232 decoder->inBufs = dsp_dce_alloc(sizeof(XDM1_BufDesc));
233 decoder->inBufs->numBufs = 1;
234 decoder->inBufs->descs[0].buf = (XDAS_Int8 *)omap_bo_dmabuf(decoder->input_bo);
235 decoder->inBufs->descs[0].bufSize = omap_bo_size(decoder->input_bo);
237 decoder->outBufs = dsp_dce_alloc(sizeof(XDM_BufDesc));
238 decoder->outBufs->numBufs = 2;
239 decoder->outBufs->bufs = (XDAS_Int8**)decoder->outBuffer;
240 decoder->outBufs->bufSizes = &decoder->outBufSizes[0];
242 decoder->inArgs = dsp_dce_alloc(sizeof(IVIDDEC2_InArgs));
243 decoder->inArgs->size = sizeof(IVIDDEC2_InArgs);
245 decoder->outArgs = dsp_dce_alloc(sizeof(IVIDDEC2_OutArgs));
246 decoder->outArgs->size = sizeof(IVIDDEC2_OutArgs);
248 return decoder;
250 usage:
251 usage(argv[0]);
252 fail:
253 if (decoder)
254 decoder_close(decoder);
255 return NULL;
256 }
258 static int
259 decoder_process(struct decoder *decoder)
260 {
261 XDM1_BufDesc *inBufs = decoder->inBufs;
262 XDM_BufDesc *outBufs = decoder->outBufs;
263 VIDDEC2_InArgs *inArgs = decoder->inArgs;
264 VIDDEC2_OutArgs *outArgs = decoder->outArgs;
265 struct buffer *buf;
266 XDAS_Int32 width = decoder->padded_width;
267 XDAS_Int32 height = decoder->padded_height;
268 XDAS_Int32 err;
269 char *src,*dst;
270 XDAS_Int32 i, success = XDAS_TRUE;
272 buf = disp_get_vid_buffer(decoder->disp);
273 if (!buf) {
274 ERROR("%p: fail: out of buffers", decoder);
275 return -1;
276 }
278 decoder->outBuffer[0] = buf->fd[0];
279 decoder->outBuffer[1] = (buf->multiplanar) ? buf->fd[1]:(XDAS_Int8 *)((decoder->outBuffer[0]));
281 decoder->outBufSizes[0] = decoder->padded_width * decoder->padded_height;
282 decoder->outBufSizes[1] = decoder->padded_width * (decoder->padded_height/2);
284 err = VIDDEC2_process(decoder->codec, inBufs, outBufs, inArgs, outArgs);
286 if (err) {
287 ERROR("%p: process returned error: %d", decoder, err);
288 return -1;
289 }
291 disp_post_vid_buffer(decoder->disp, buf, 0, 0, width, height);
292 /* Display the output buffer for 10 seconds */
293 sleep(10);
295 disp_put_vid_buffer(decoder->disp, buf);
297 src = (char*)decoder->input;
298 dst = (char*)omap_bo_map(buf->bo[0]);
300 MSG("Comparing output buffer with input buffer\n");
302 for(i = 0; i < decoder->input_sz; i++){
303 if(*src != *dst){
304 MSG("yuvcopytest failed at %d, 0x%x, 0x%x\n", i, *src, *dst);
305 success = XDAS_FALSE;
306 break;
307 }
308 src++;
309 dst++;
310 }
312 if(success ==XDAS_TRUE)
313 MSG("yuvcopytest executed successfully\n");
315 return 0;
316 }
318 int
319 main(int argc, char **argv)
320 {
321 struct decoder *decoder = NULL;
322 int i;
323 for (i = 1; i < argc; i++)
324 if ( !strcmp(argv[i], "--help")) {
325 usage(argv[0]);
326 exit(0);
327 }
328 decoder = decoder_open(argc,argv);
330 if(decoder)
331 decoder_process(decoder);
333 if(decoder)
334 decoder_close(decoder);
336 decoder = NULL;
338 return 0;
339 }