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