Add disp_close, disp_x11_close
[gstreamer-omap:omapdrmtest.git] / viddec3test.c
1 /*
2  * Copyright (C) 2012 Texas Instruments
3  * Author: Rob Clark <rob.clark@linaro.org>
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
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21
22 #include <dce.h>
23 #include <xdc/std.h>
24 #include <ti/xdais/xdas.h>
25 #include <ti/sdo/ce/Engine.h>
26 #include <ti/sdo/ce/video3/viddec3.h>
27
28 #include "util.h"
29 #include "demux.h"
30
31 /* Padding for width as per Codec Requirement (for h264) */
32 #define PADX  32
33 /* Padding for height as per Codec requirement (for h264)*/
34 #define PADY  24
35
36 struct decoder {
37         struct display *disp;
38         struct demux *demux;
39         struct buffer *framebuf;
40
41         Engine_Handle engine;
42         VIDDEC3_Handle codec;
43         VIDDEC3_Params *params;
44         VIDDEC3_DynamicParams *dynParams;
45         VIDDEC3_Status *status;
46         XDM2_BufDesc *inBufs;
47         XDM2_BufDesc *outBufs;
48         VIDDEC3_InArgs *inArgs;
49         VIDDEC3_OutArgs *outArgs;
50
51         char *input;
52         struct omap_bo *input_bo;
53         int input_sz;
54
55         suseconds_t tdisp;
56
57 };
58
59 static void
60 usage(char *name)
61 {
62         MSG("Usage: %s [OPTIONS] INFILE", name);
63         MSG("Test of viddec3 decoder.");
64         MSG("");
65         disp_usage();
66 }
67
68 static void
69 decoder_close(struct decoder *decoder)
70 {
71         if (decoder->codec)          VIDDEC3_delete(decoder->codec);
72         if (decoder->engine)         Engine_close(decoder->engine);
73         if (decoder->params)         dce_free(decoder->params);
74         if (decoder->dynParams)      dce_free(decoder->dynParams);
75         if (decoder->status)         dce_free(decoder->status);
76         if (decoder->inBufs)         free(decoder->inBufs);
77         if (decoder->outBufs)        free(decoder->outBufs);
78         if (decoder->inArgs)         dce_free(decoder->inArgs);
79         if (decoder->outArgs)        dce_free(decoder->outArgs);
80         if (decoder->input_bo)       omap_bo_del(decoder->input_bo);
81         if (decoder->demux)          demux_deinit(decoder->demux);
82         if (decoder->disp)           disp_close(decoder->disp);
83
84         free(decoder);
85 }
86
87 static struct decoder *
88 decoder_open(int argc, char **argv)
89 {
90         struct decoder *decoder;
91         char *infile = NULL;
92         int i, num_buffers;
93         int width, height, padded_width, padded_height;
94         Engine_Error ec;
95         XDAS_Int32 err;
96
97         decoder = calloc(1, sizeof(*decoder));
98         if (!decoder)
99                 return NULL;
100
101         MSG("%p: Opening Display..", decoder);
102         decoder->disp = disp_open(argc, argv);
103         if (!decoder->disp)
104                 goto usage;
105
106         /* loop thru args, find input file.. */
107         for (i = 1; i < argc; i++) {
108                 int fd;
109                 if (!argv[i]) {
110                         continue;
111                 }
112                 fd = open(argv[i], 0);
113                 if (fd > 0) {
114                         infile = argv[i];
115                         argv[i] = NULL;
116                         close(fd);
117                         break;
118                 }
119                 break;
120         }
121
122         if (check_args(argc, argv) || !infile)
123                 goto usage;
124
125         MSG("%p: Opening Demuxer..", decoder);
126         decoder->demux = demux_init(infile, &width, &height);
127         if (!decoder->demux) {
128                 ERROR("%p: could not open demuxer", decoder);
129                 goto fail;
130         }
131
132         MSG("%p: infile=%s, width=%d, height=%d", decoder, infile, width, height);
133
134         /* calculate output buffer parameters: */
135         width  = ALIGN2 (width, 4);        /* round up to macroblocks */
136         height = ALIGN2 (height, 4);       /* round up to macroblocks */
137         padded_width  = ALIGN2 (width + (2*PADX), 7);
138         padded_height = height + 4*PADY;
139         num_buffers   = MIN(16, 32768 / ((width/16) * (height/16))) + 3;
140
141         MSG("%p: padded_width=%d, padded_height=%d, num_buffers=%d",
142                         decoder, padded_width, padded_height, num_buffers);
143
144         decoder->input_sz = width * height;
145         decoder->input_bo = omap_bo_new(decoder->disp->dev,
146                         decoder->input_sz, OMAP_BO_WC);
147         decoder->input = omap_bo_map(decoder->input_bo);
148
149         decoder->framebuf = disp_get_fb(decoder->disp);
150
151         if (! disp_get_vid_buffers(decoder->disp, num_buffers,
152                         FOURCC_STR("NV12"), padded_width, padded_height)) {
153                 ERROR("%p: could not allocate buffers", decoder);
154                 goto fail;
155         }
156
157         MSG("%p: Opening Engine..", decoder);
158         dce_set_fd(decoder->disp->fd);
159         decoder->engine = Engine_open("ivahd_vidsvr", NULL, &ec);
160         if (!decoder->engine) {
161                 ERROR("%p: could not open engine", decoder);
162                 goto fail;
163         }
164
165         decoder->params = dce_alloc(sizeof(IVIDDEC3_Params));
166         decoder->params->size = sizeof(IVIDDEC3_Params);
167
168         decoder->params->maxWidth         = width;
169         decoder->params->maxHeight        = height;
170         decoder->params->maxFrameRate     = 30000;
171         decoder->params->maxBitRate       = 10000000;
172         decoder->params->dataEndianness   = XDM_BYTE;
173         decoder->params->forceChromaFormat= XDM_YUV_420SP;
174         decoder->params->operatingMode    = IVIDEO_DECODE_ONLY;
175         decoder->params->displayDelay     = IVIDDEC3_DISPLAY_DELAY_AUTO;
176         decoder->params->displayBufsMode  = IVIDDEC3_DISPLAYBUFS_EMBEDDED;
177 MSG("displayBufsMode: %d", decoder->params->displayBufsMode);
178         decoder->params->inputDataMode    = IVIDEO_ENTIREFRAME;
179         decoder->params->metadataType[0]  = IVIDEO_METADATAPLANE_NONE;
180         decoder->params->metadataType[1]  = IVIDEO_METADATAPLANE_NONE;
181         decoder->params->metadataType[2]  = IVIDEO_METADATAPLANE_NONE;
182         decoder->params->numInputDataUnits= 0;
183         decoder->params->outputDataMode   = IVIDEO_ENTIREFRAME;
184         decoder->params->numOutputDataUnits = 0;
185         decoder->params->errorInfoMode    = IVIDEO_ERRORINFO_OFF;
186
187         decoder->codec = VIDDEC3_create(decoder->engine,
188                         "ivahd_h264dec", decoder->params);
189         if (!decoder->codec) {
190                 ERROR("%p: could not create codec", decoder);
191                 goto fail;
192         }
193
194         decoder->dynParams = dce_alloc(sizeof(IVIDDEC3_DynamicParams));
195         decoder->dynParams->size = sizeof(IVIDDEC3_DynamicParams);
196
197         decoder->dynParams->decodeHeader  = XDM_DECODE_AU;
198
199         /*Not Supported: Set default*/
200         decoder->dynParams->displayWidth  = 0;
201         decoder->dynParams->frameSkipMode = IVIDEO_NO_SKIP;
202         decoder->dynParams->newFrameFlag  = XDAS_TRUE;
203
204         decoder->status = dce_alloc(sizeof(IVIDDEC3_Status));
205         decoder->status->size = sizeof(IVIDDEC3_Status);
206
207         err = VIDDEC3_control(decoder->codec, XDM_SETPARAMS,
208                         decoder->dynParams, decoder->status);
209         if (err) {
210                 ERROR("%p: fail: %d", decoder, err);
211                 goto fail;
212         }
213
214         /* not entirely sure why we need to call this here.. just copying omx.. */
215         err = VIDDEC3_control(decoder->codec, XDM_GETBUFINFO,
216                         decoder->dynParams, decoder->status);
217         if (err) {
218                 ERROR("%p: fail: %d", decoder, err);
219                 goto fail;
220         }
221
222         decoder->inBufs = calloc(1, sizeof(XDM2_BufDesc));
223         decoder->inBufs->numBufs = 1;
224         decoder->inBufs->descs[0].buf =
225                         (XDAS_Int8 *)omap_bo_handle(decoder->input_bo);
226         decoder->inBufs->descs[0].bufSize.bytes = omap_bo_size(decoder->input_bo);
227         decoder->inBufs->descs[0].memType = XDM_MEMTYPE_BO;
228
229         decoder->outBufs = calloc(1, sizeof(XDM2_BufDesc));
230         decoder->outBufs->numBufs = 2;
231         decoder->outBufs->descs[0].memType = XDM_MEMTYPE_BO;
232         decoder->outBufs->descs[1].memType = XDM_MEMTYPE_BO;
233
234         decoder->inArgs = dce_alloc(sizeof(IVIDDEC3_InArgs));
235         decoder->inArgs->size = sizeof(IVIDDEC3_InArgs);
236
237         decoder->outArgs = dce_alloc(sizeof(IVIDDEC3_OutArgs));
238         decoder->outArgs->size = sizeof(IVIDDEC3_OutArgs);
239
240         decoder->tdisp = mark(NULL);
241
242         return decoder;
243
244 usage:
245         usage(argv[0]);
246 fail:
247         if (decoder)
248                 decoder_close(decoder);
249         return NULL;
250 }
251
252 static int
253 decoder_process(struct decoder *decoder)
254 {
255         XDM2_BufDesc *inBufs = decoder->inBufs;
256         XDM2_BufDesc *outBufs = decoder->outBufs;
257         VIDDEC3_InArgs *inArgs = decoder->inArgs;
258         VIDDEC3_OutArgs *outArgs = decoder->outArgs;
259         XDAS_Int32 err;
260         struct buffer *buf;
261         int i, n;
262         suseconds_t tproc;
263
264         buf = disp_get_vid_buffer(decoder->disp);
265         if (!buf) {
266                 ERROR("%p: fail: out of buffers", decoder);
267                 return -1;
268         }
269
270         n = demux_read(decoder->demux, decoder->input, decoder->input_sz);
271         if (n) {
272                 inBufs->descs[0].bufSize.bytes = n;
273                 inArgs->numBytes = n;
274                 MSG("%p: push: %d bytes (%p)", decoder, n, buf);
275         } else {
276                 /* end of input.. do we need to flush? */
277                 MSG("%p: end of input", decoder);
278                 inBufs->numBufs = 0;
279                 inArgs->inputID = 0;
280         }
281
282         inArgs->inputID = (XDAS_Int32)buf;
283         outBufs->descs[0].buf = (XDAS_Int8 *)omap_bo_handle(buf->bo[0]);
284         outBufs->descs[0].bufSize.bytes = omap_bo_size(buf->bo[0]);
285         outBufs->descs[1].buf = (XDAS_Int8 *)omap_bo_handle(buf->bo[1]);
286         outBufs->descs[1].bufSize.bytes = omap_bo_size(buf->bo[1]);
287
288         tproc = mark(NULL);
289         err = VIDDEC3_process(decoder->codec, inBufs, outBufs, inArgs, outArgs);
290         MSG("%p: processed returned in: %ldus", decoder, (long int)mark(&tproc));
291         if (err) {
292                 ERROR("%p: process returned error: %d", decoder, err);
293                 ERROR("%p: extendedError: %08x", decoder, outArgs->extendedError);
294                 if (XDM_ISFATALERROR(outArgs->extendedError))
295                         return -1;
296         }
297
298         for (i = 0; outArgs->outputID[i]; i++) {
299                 /* calculate offset to region of interest */
300                 XDM_Rect *r = &(outArgs->displayBufs.bufDesc[0].activeFrameRegion);
301
302                 /* get the output buffer and write it to file */
303                 buf = (struct buffer *)outArgs->outputID[i];
304                 MSG("%p: post buffer: %p %d,%d %d,%d", decoder, buf,
305                                 r->topLeft.x, r->topLeft.y,
306                                 r->bottomRight.x, r->bottomRight.y);
307                 disp_post_vid_buffer(decoder->disp, buf,
308                                 r->topLeft.x, r->topLeft.y,
309                                 r->bottomRight.x - r->topLeft.x,
310                                 r->bottomRight.y - r->topLeft.y);
311                 MSG("%p: display in: %ldus", decoder, (long int)mark(&decoder->tdisp));
312         }
313
314         for (i = 0; outArgs->freeBufID[i]; i++) {
315                 buf = (struct buffer *)outArgs->freeBufID[i];
316                 disp_put_vid_buffer(decoder->disp, buf);
317         }
318
319         if (outArgs->outBufsInUseFlag) {
320                 MSG("%p: TODO... outBufsInUseFlag", decoder); // XXX
321         }
322
323         return (inBufs->numBufs > 0) ? 0 : -1;
324 }
325
326 int
327 main(int argc, char **argv)
328 {
329         struct decoder *decoders[8] = {};
330         int i, n, first = 0, ndecoders = 0;
331
332         for (i = 1; i < argc; i++) {
333                 if (!strcmp(argv[i], "--")) {
334                         argv[first] = argv[0];
335                         decoders[ndecoders++] = decoder_open(i - first, &argv[first]);
336                         first = i;
337                 }
338         }
339
340         argv[first] = argv[0];
341         decoders[ndecoders++] = decoder_open(i - first, &argv[first]);
342
343         do {
344                 for (i = 0, n = 0; i < ndecoders; i++) {
345                         if (decoders[i]) {
346                                 int ret = decoder_process(decoders[i]);
347                                 if (ret) {
348                                         decoder_close(decoders[i]);
349                                         decoders[i] = NULL;
350                                         continue;
351                                 }
352                                 n++;
353                         }
354                 }
355         } while(n > 0);
356
357         return 0;
358 }