]> Gitweb @ Texas Instruments - Open Source Git Repositories - git.TI.com/gitweb - glsdk/omapdrmtest.git/blob - viddec3test.c
Add signal handler for viddec3test and videnc2test
[glsdk/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 #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>
24 #include <signal.h>
25 #include <unistd.h>
27 #include <pthread.h>
29 #include "util.h"
30 #include "demux.h"
32 /* Padding for width as per Codec Requirement (for h264) */
33 #define PADX  32
34 /* Padding for height as per Codec requirement (for h264)*/
35 #define PADY  24
36 /* omap drm device handle */
37 struct omap_device *dev = NULL;
39 struct decoder {
40         struct display *disp;
41         struct demux *demux;
42         struct buffer *framebuf;
43         Engine_Handle engine;
44         VIDDEC3_Handle codec;
45         VIDDEC3_Params *params;
46         VIDDEC3_DynamicParams *dynParams;
47         VIDDEC3_Status *status;
48         XDM2_BufDesc *inBufs;
49         XDM2_BufDesc *outBufs;
50         VIDDEC3_InArgs *inArgs;
51         VIDDEC3_OutArgs *outArgs;
52         char *input;
53         struct omap_bo *input_bo;
54         int input_sz, uv_offset;
55         int padded_width;
56         int padded_height;
57         int num_outBuf;
58         size_t *outBuf_fd;
59         suseconds_t tdisp;
60         int id;
61 };
64 struct decoder *decoders[8] = {};
65 int ndecoders = 0;
67 /* When true, do not actually call VIDDEC3_process. For benchmarking. */
68 static int no_process = 0;
69 static int inloop = 0;
71 /* When true, loop at end of playback. */
72 static int loop = 0;
74 static void
75 usage(char *name)
76 {
77         MSG("Usage: %s [OPTIONS] INFILE", name);
78         MSG("Test of viddec3 decoder.");
79         MSG("");
80         MSG("viddec3test options:");
81         MSG("\t-h, --help: Print this help and exit.");
82         MSG("\t--loop\tRestart playback at end of stream.");
83         MSG("\t--inloop\tRestart playback at end of stream along with decoder reinitialization.");
84         MSG("\t--no-process\tDo not actually call VIDDEC3_process method. For benchmarking.");
85         MSG("");
86         disp_usage();
87 }
89 static void
90 decoder_close(struct decoder *decoder)
91 {
92         if(!decoder) return;
93         /* free output buffers allocated by display */
94         if(inloop < 2 && decoder->disp) disp_free_buffers(decoder->disp,decoder->num_outBuf);
96         if (decoder->status)         dce_free(decoder->status);
97         if (decoder->params)         dce_free(decoder->params);
98         if (decoder->dynParams)      dce_free(decoder->dynParams);
99         if (decoder->inBufs) {
100                 dce_buf_unlock(1, &(decoder->inBufs->descs[0].buf));
101                 close(decoder->inBufs->descs[0].buf);
102                 dce_free(decoder->inBufs);
103         }
104         if (decoder->outBufs)        dce_free(decoder->outBufs);
105         if (decoder->inArgs)         dce_free(decoder->inArgs);
106         if (decoder->outArgs)        dce_free(decoder->outArgs);
107         if (decoder->codec)          VIDDEC3_delete(decoder->codec);
108         if (decoder->engine)         Engine_close(decoder->engine);
109     if (decoder->input_bo)       omap_bo_del(decoder->input_bo);
110         if (decoder->outBuf_fd)      free(decoder->outBuf_fd);
111         if(inloop < 2) {
112                 if (dev)                             dce_deinit(dev);
113                 if (decoder->demux)          demux_deinit(decoder->demux);
114                 if (decoder->disp)           disp_close(decoder->disp);
115                 if(decoder) {
116                   free(decoder);
117                  }
118         }
121 static struct decoder *
122 decoder_open(int argc, char **argv)
124         struct decoder *decoder = NULL;
125         char *infile = NULL;
126         int i;
127         int width, height, padded_width, padded_height;
128         Engine_Error ec;
129         XDAS_Int32 err;
131     if(inloop < 2) {
132                 decoder = calloc(1, sizeof(*decoder));
133                 if (!decoder)
134                         return NULL;
136                 MSG("%p: Opening Display..", decoder);
137                 decoder->disp = disp_open(argc, argv);
138                 if (!decoder->disp)
139                         goto usage;
141             /* loop thru args, find input file.. */
142                 for (i = 1; i < argc; i++) {
143                         int fd;
144                             if (!argv[i]) {
145                                     continue;
146                         }
147                         fd = open(argv[i], 0);
148                         if (fd > 0) {
149                                 infile = argv[i];
150                                 argv[i] = NULL;
151                                 close(fd);
152                                 break;
153                         }
154                         break;
155                 }
156                 if (check_args(argc, argv) || !infile)
157                         goto usage;
158                 MSG("%p: Opening Demuxer..", decoder);
159                 decoder->demux = demux_init(infile, &width, &height);
160                 if (!decoder->demux) {
161                         ERROR("%p: could not open demuxer", decoder);
162                         goto fail;
163                 }
164                 MSG("%p: infile=%s, width=%d, height=%d", decoder, infile, width, height);
166                 /* calculate output buffer parameters: */
167                 width  = ALIGN2 (width, 4);        /* round up to macroblocks */
168                 height = ALIGN2 (height, 4);       /* round up to macroblocks */
169                 if (decoder->demux->cc->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
170                         padded_width = width;
171                         padded_height= height;
172                 }
173                 else {
174                         padded_width  = ALIGN2 (width + (2*PADX), 7);
175                         padded_height = height + 4*PADY;
176                 }
177                 decoder->num_outBuf   = MIN(16, 32768 / ((width/16) * (height/16))) + 3;
178                 decoder->padded_width = padded_width;
179                 decoder->padded_height = padded_height;
180                 MSG("%p: padded_width=%d, padded_height=%d, num_buffers=%d",
181                         decoder, padded_width, padded_height, decoder->num_outBuf);
182                 dce_set_fd(decoder->disp->fd);
183                 dev = dce_init();
184                 if(dev == NULL) {
185                         ERROR("%p: dce init failed", dev);
186                         goto fail;
187                 }
188                 decoder->framebuf = disp_get_fb(decoder->disp);
189                 if (! disp_get_vid_buffers(decoder->disp, decoder->num_outBuf,
190                                 FOURCC_STR("NV12"), decoder->padded_width, decoder->padded_height)) {
191                         ERROR("%p: could not allocate buffers", decoder);
192                         goto fail;
193                 }
194                 if(inloop) inloop = 2; /*Don't bother about looping if not asked to*/
195     }
197         if (!decoder->disp->multiplanar) {
198                 decoder->uv_offset = padded_width * padded_height;
199                 decoder->outBuf_fd = malloc(sizeof(int)*decoder->num_outBuf);
200                 MSG("%p: uv_offset=%d", decoder, decoder->uv_offset);
201         }
202         else{
203                 decoder->outBuf_fd = malloc(sizeof(int)*(decoder->num_outBuf*2));
204         }
206         decoder->input_sz = width * height;
207         decoder->input_bo = omap_bo_new(decoder->disp->dev,
208                         decoder->input_sz, OMAP_BO_WC);
209         decoder->input = omap_bo_map(decoder->input_bo);
213         MSG("%p: Opening Engine..", decoder);
214         decoder->engine = Engine_open("ivahd_vidsvr", NULL, &ec);
215         if (!decoder->engine) {
216                 ERROR("%p: could not open engine", decoder);
217                 goto fail;
218         }
220         decoder->params = dce_alloc(sizeof(IVIDDEC3_Params));
221         decoder->params->size = sizeof(IVIDDEC3_Params);
223         decoder->params->maxWidth         = width;
224         decoder->params->maxHeight        = height;
225         decoder->params->maxFrameRate     = 30000;
226         decoder->params->maxBitRate       = 10000000;
227         decoder->params->dataEndianness   = XDM_BYTE;
228         decoder->params->forceChromaFormat= XDM_YUV_420SP;
229         decoder->params->operatingMode    = IVIDEO_DECODE_ONLY;
230         decoder->params->displayDelay     = IVIDDEC3_DISPLAY_DELAY_AUTO;
231         decoder->params->displayBufsMode  = IVIDDEC3_DISPLAYBUFS_EMBEDDED;
232         MSG("displayBufsMode: %d", decoder->params->displayBufsMode);
233         decoder->params->inputDataMode    = IVIDEO_ENTIREFRAME;
234         decoder->params->metadataType[0]  = IVIDEO_METADATAPLANE_NONE;
235         decoder->params->metadataType[1]  = IVIDEO_METADATAPLANE_NONE;
236         decoder->params->metadataType[2]  = IVIDEO_METADATAPLANE_NONE;
237         decoder->params->numInputDataUnits= 0;
238         decoder->params->outputDataMode   = IVIDEO_ENTIREFRAME;
239         decoder->params->numOutputDataUnits = 0;
240         decoder->params->errorInfoMode    = IVIDEO_ERRORINFO_OFF;
242         if (decoder->demux->cc->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
243             decoder->codec = VIDDEC3_create(decoder->engine,
244                                         "ivahd_mpeg2vdec", decoder->params);
245         }
246         else if (decoder->demux->cc->codec_id == AV_CODEC_ID_H264) {
247             decoder->codec = VIDDEC3_create(decoder->engine,
248                                         "ivahd_h264dec", decoder->params);
249         }
250         else if (decoder->demux->cc->codec_id == AV_CODEC_ID_MPEG4) {
251                 decoder->demux->first_in_buff = 1;
252                 decoder->codec = VIDDEC3_create(decoder->engine,
253                                         "ivahd_mpeg4dec", decoder->params);
254         }
256         if (!decoder->codec) {
257                 ERROR("%p: could not create codec", decoder);
258                 goto fail;
259         }
261         decoder->dynParams = dce_alloc(sizeof(IVIDDEC3_DynamicParams));
262         decoder->dynParams->size = sizeof(IVIDDEC3_DynamicParams);
264         decoder->dynParams->decodeHeader  = XDM_DECODE_AU;
266         /*Not Supported: Set default*/
267         decoder->dynParams->displayWidth  = 0;
268         decoder->dynParams->frameSkipMode = IVIDEO_NO_SKIP;
269         decoder->dynParams->newFrameFlag  = XDAS_TRUE;
271         decoder->status = dce_alloc(sizeof(IVIDDEC3_Status));
272         decoder->status->size = sizeof(IVIDDEC3_Status);
274         err = VIDDEC3_control(decoder->codec, XDM_SETPARAMS,
275                         decoder->dynParams, decoder->status);
276         if (err) {
277                 ERROR("%p: fail: %d", decoder, err);
278                 goto fail;
279         }
281         /* not entirely sure why we need to call this here.. just copying omx.. */
282         err = VIDDEC3_control(decoder->codec, XDM_GETBUFINFO,
283                         decoder->dynParams, decoder->status);
284         if (err) {
285                 ERROR("%p: fail: %d", decoder, err);
286                 goto fail;
287         }
289         decoder->inBufs = dce_alloc(sizeof(XDM2_BufDesc));
290         decoder->inBufs->numBufs = 1;
291         decoder->inBufs->descs[0].buf = (XDAS_Int8 *)omap_bo_dmabuf(decoder->input_bo);
292         dce_buf_lock(1, &(decoder->inBufs->descs[0].buf));
293         decoder->inBufs->descs[0].bufSize.bytes = omap_bo_size(decoder->input_bo);
294         decoder->inBufs->descs[0].memType = XDM_MEMTYPE_RAW;
297         decoder->outBufs = dce_alloc(sizeof(XDM2_BufDesc));
298         decoder->outBufs->numBufs = 2;
299         decoder->outBufs->descs[0].memType = XDM_MEMTYPE_RAW;
300         decoder->outBufs->descs[1].memType = XDM_MEMTYPE_RAW;
302         decoder->inArgs = dce_alloc(sizeof(IVIDDEC3_InArgs));
303         decoder->inArgs->size = sizeof(IVIDDEC3_InArgs);
305         decoder->outArgs = dce_alloc(sizeof(IVIDDEC3_OutArgs));
306         decoder->outArgs->size = sizeof(IVIDDEC3_OutArgs);
308         decoder->tdisp = mark(NULL);
310         return decoder;
312 usage:
313         usage(argv[0]);
314 fail:
315         if(inloop) inloop = 1; /*Error case: delete everything*/
316         if (decoder)
317                 decoder_close(decoder);
318         return NULL;
321 static int
322 decoder_process(struct decoder *decoder)
324         XDM2_BufDesc *inBufs = decoder->inBufs;
325         XDM2_BufDesc *outBufs = decoder->outBufs;
326         VIDDEC3_InArgs *inArgs = decoder->inArgs;
327         VIDDEC3_OutArgs *outArgs = decoder->outArgs;
328         struct buffer *buf;
329         int freeBufCount =0;
330         int i, n;
331         XDAS_Int32 err;
332         int eof = 0; /* end of file flag */
334         /* demux; in loop mode, we can do two tries at the end of the stream. */
335         for (i = 0; i < 2; i++) {
336                 n = demux_read(decoder->demux, decoder->input, decoder->input_sz);
337                 if (n) {
338                         buf = disp_get_vid_buffer(decoder->disp);
339                         if (!buf) {
340                                 ERROR("%p: fail: out of buffers", decoder);
341                                 return -1;
342                         }
343                         inBufs->descs[0].bufSize.bytes = n;
344                         inArgs->numBytes = n;
345                         DBG("%p: push: %d bytes (%p)", decoder, n, buf);
346                 } else {
347                         /* end of input.. do we need to flush? */
348                         MSG("%p: end of input", decoder);
350                         /* In loop mode: rewind and retry once. */
351                         if (loop && i == 0) {
352                                 int err = demux_rewind(decoder->demux);
353                                 if (err < 0) {
354                                         ERROR("%p: demux_rewind returned error: %d", decoder, err);
355                                         return -1;
356                                 }
357                                 MSG("%p: rewound.", decoder);
358                                 if(!inloop) continue;
359                         }
360                         eof = 1; /* set the flag for end of file to 1 */
361                         /* Control call call with XDM_FLUSH command */
362                         err = VIDDEC3_control(decoder->codec, XDM_FLUSH,
363                                         decoder->dynParams, decoder->status);
364                         inBufs->numBufs = 0;
365                         outBufs->numBufs = 0;
366                         inArgs->inputID = 0;
367                 }
368                 break;
369         }
371         /*set the parameters if it is not the end of file */
372         if (!eof) {
373                 inArgs->inputID = (XDAS_Int32)buf;
374                 outBufs->descs[0].buf = buf->fd[0];
375                 outBufs->descs[1].buf = (buf->multiplanar) ?buf->fd[1]:(XDAS_Int8 *)((outBufs->descs[0].buf));
378                 if(buf->multiplanar){
379                         decoder->outBuf_fd[0] = buf->fd[0];
380                         decoder->outBuf_fd[1] = buf->fd[1];
381             dce_buf_lock(2,decoder->outBuf_fd);
382                 }
383                 else{
384                         decoder->outBuf_fd[0] = buf->fd[0];
385                         dce_buf_lock(1,decoder->outBuf_fd);
386                 }
387                 decoder->outBufs->descs[0].bufSize.bytes =decoder->padded_width*decoder->padded_height;
388                 decoder->outBufs->descs[1].bufSize.bytes = decoder->padded_width* (decoder->padded_height/2);
389         }
391         do {
392                 if (no_process) {
393                         /* Do not process. This is for benchmarking. We need to "fake"
394                          * the outArgs. */
395                         outArgs->outputID[0] = 0;
396                         outArgs->freeBufID[0] = 0;
397                         if(!eof) {
398                                 outArgs->outputID[0] = buf;
399                                 outArgs->freeBufID[0] = buf;
400                         }
401                         outArgs->outputID[1] = NULL;
402                         outArgs->freeBufID[1] = NULL;
403                         outArgs->outBufsInUseFlag = 0;
405                 } else {
406                         suseconds_t tproc;
407                         tproc = mark(NULL);
408                         err = VIDDEC3_process(decoder->codec, inBufs, outBufs, inArgs, outArgs);
409                         DBG("%p: processed returned in: %ldus", decoder, (long int)mark(&tproc));
410                         if (err) {
411                                 ERROR("%p: process returned error: %d", decoder, err);
412                                 ERROR("%p: extendedError: %08x", decoder, outArgs->extendedError);
413                                 if (XDM_ISFATALERROR(outArgs->extendedError) || ( err == DCE_EXDM_UNSUPPORTED ) || ( err == DCE_EIPC_CALL_FAIL ) || ( err == DCE_EINVALID_INPUT ))
414                                         return -1;
415                         }
416                 }
418                 for (i = 0; outArgs->outputID[i]; i++) {
419                         /* calculate offset to region of interest */
420                         XDM_Rect *r = &(outArgs->displayBufs.bufDesc[0].activeFrameRegion);
422                         /* get the output buffer and write it to file */
423                         buf = (struct buffer *)outArgs->outputID[i];
424                         if(!no_process)
425                                disp_post_vid_buffer(decoder->disp, buf,
426                                         r->topLeft.x, r->topLeft.y,
427                                         r->bottomRight.x - r->topLeft.x,
428                                         r->bottomRight.y - r->topLeft.y);
429                 }
431                 for (i = 0; outArgs->freeBufID[i]; i++) {
432                         buf = (struct buffer *)outArgs->freeBufID[i];
433                         disp_put_vid_buffer(decoder->disp, buf);
435                         if(buf->multiplanar){
436                                 decoder->outBuf_fd[freeBufCount++] = buf->fd[0];
437                                 decoder->outBuf_fd[freeBufCount++] = buf->fd[1];
438                         }
439                         else{
440                                 decoder->outBuf_fd[freeBufCount++] = buf->fd[0];
441                                 DBG("FreeBufID: %p fd:%d\n", outArgs->freeBufID[i], buf->fd[0]);
442                         }
443                 }
445                 if(freeBufCount){
446             if(!eof)dce_buf_unlock(freeBufCount,decoder->outBuf_fd);
447                         freeBufCount =0;
448                 }
449                 if (outArgs->outBufsInUseFlag) {
450                         MSG("%p: TODO... outBufsInUseFlag", decoder); // XXX
451                 }
452         } while ((err == 0) && eof && !no_process);
454         return (inBufs->numBufs > 0) ? 0 : -1;
457 /* Returns 1 (true) if the mutex is unlocked, which is the
458  * thread's signal to terminate.
459   */
461 pthread_mutex_t mtx;
463 int needQuit()
464  {
465    switch(pthread_mutex_trylock(&mtx)) {
466     case 0: /* if we got the lock, unlock and return 1 (true) */
467               pthread_mutex_unlock(&mtx);
468           return 1;
469     case EBUSY: /* return 0 (false) if the mutex was locked */
470               return 0;
471    }
472   return 1;
473   }
475 void *decode_stream(void *decoderHandle)
477         int ret = 0;
478         struct decoder *decoder = (struct decoder*)decoderHandle;
479     int n = 0;
480     if(!decoder) goto exit;
482     while((ret = decoder_process(decoder)) == 0) {
483            if(needQuit()){
484               inloop = 1;
485               break;
486             }
487          }
488     if((ret != -1 && ret != 0) && inloop) inloop = 1; /*Assuming Good case. Otherwise logic gets messy*/
489     int i = decoder->id;
490         decoder_close(decoder);
491         decoders[i]=NULL;
492 exit:
493         return NULL;
497 static void sig_handler(int signo)
499   if (signo == SIGINT) {
500           int i=0;
501           pthread_mutex_unlock(&mtx);
502           sleep(1);
503           exit(0);
504   }
508 int
509 main(int argc, char **argv)
512    int i, first = 0;
514    struct sigaction sa;
516    sa.sa_handler = sig_handler;
517    sigemptyset(&sa.sa_mask);
518    sa.sa_flags = 0;
520    if (sigaction(SIGINT, &sa, NULL) == -1) {
521      ERROR ("\nDid not catch  SIGINT\n");
522    }
523    signal(SIGINT,sig_handler);
525    pthread_mutex_init(&mtx,NULL);
526    pthread_mutex_lock(&mtx);
528     for (i = 1; i < argc; i++) {
529                 if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
530                         usage(argv[0]);
531                         exit(0);
533                 } else if (!strcmp(argv[i], "--loop")) {
534                         loop = 1;
535                         argv[i] = NULL;
537                 } else if (!strcmp(argv[i], "--no-process")) {
538                         no_process = 1;
539                         argv[i] = NULL;
541                 } else if (!strcmp(argv[i], "--inloop")) {
542                         inloop = 1; // this means inloop is detected
543                         DBG("detected inloop = %d\n", inloop);
544                         loop = 1; //we want rewind as well
545                         argv[i] = NULL;
546                 } else if (!strcmp(argv[i], "--")) {
547                         argv[first] = argv[0];
548                         decoders[ndecoders] = decoder_open(i - first, &argv[first]);
549                         decoders[ndecoders]->id = ndecoders;
550                         ndecoders++;
551                         first = i;
552                 }
553         }
555         argv[first] = argv[0];
556         argc = i - first;
558         if(ndecoders) {
559             decoders[ndecoders] = decoder_open(argc ,&argv[first]);
560                 decoders[ndecoders]->id = ndecoders;
561                 ndecoders++;
562         }
564         if (ndecoders > 1) {
565                 pthread_t threadIds[8];
567                 for (i = 0; i < ndecoders; i++) {
568                         if (decoders[i]) {
569                                 int ret = pthread_create(&threadIds[i], NULL,
570                                                 &decode_stream, decoders[i]);
571                                 if (ret != 0)
572                                         ERROR("%p: creation of pthread, error: %d",
573                                                         decoders[i], ret);
574                         }
575                 }
577                 for (i = 0; i < ndecoders; i++) {
578                         pthread_join(threadIds[i], NULL);
579                 }
580         }
581         else {
582                 int itr = 0;
583                 do {
584                         decoders[0] = decoder_open(argc, &argv[first]);
585                         decoders[0]->id = 0;
586                         ndecoders++;
587                         decode_stream(decoders[0]);
588                         if (inloop) {
589                                 MSG("=================Iteration %d complete =============== %d\n", ++itr);
590                         }
591                 }while(inloop);
592         }
594         return 0;