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