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>
25 #include <pthread.h>
27 #include "util.h"
28 #include "demux.h"
30 /* Used for mpeg4 esds data copy */
31 int first_in_buff = 0;
33 /* Padding for width as per Codec Requirement (for h264) */
34 #define PADX 32
35 /* Padding for height as per Codec requirement (for h264)*/
36 #define PADY 24
37 /* omap drm device handle */
38 struct omap_device *dev = NULL;
40 struct decoder {
41 struct display *disp;
42 struct demux *demux;
43 struct buffer *framebuf;
44 Engine_Handle engine;
45 VIDDEC3_Handle codec;
46 VIDDEC3_Params *params;
47 VIDDEC3_DynamicParams *dynParams;
48 VIDDEC3_Status *status;
49 XDM2_BufDesc *inBufs;
50 XDM2_BufDesc *outBufs;
51 VIDDEC3_InArgs *inArgs;
52 VIDDEC3_OutArgs *outArgs;
53 char *input;
54 struct omap_bo *input_bo;
55 int input_sz, uv_offset;
56 int padded_width;
57 int padded_height;
58 int num_outBuf;
59 size_t *outBuf_fd;
60 suseconds_t tdisp;
61 };
63 /* When true, do not actually call VIDDEC3_process. For benchmarking. */
64 static int no_process = 0;
65 static int inloop = 0;
67 /* When true, loop at end of playback. */
68 static int loop = 0;
70 static void
71 usage(char *name)
72 {
73 MSG("Usage: %s [OPTIONS] INFILE", name);
74 MSG("Test of viddec3 decoder.");
75 MSG("");
76 MSG("viddec3test options:");
77 MSG("\t-h, --help: Print this help and exit.");
78 MSG("\t--loop\tRestart playback at end of stream.");
79 MSG("\t--inloop\tRestart playback at end of stream along with decoder reinitialization.");
80 MSG("\t--no-process\tDo not actually call VIDDEC3_process method. For benchmarking.");
81 MSG("");
82 disp_usage();
83 }
85 static void
86 decoder_close(struct decoder *decoder)
87 {
88 if(!decoder) return;
89 /* free output buffers allocated by display */
90 if(inloop < 2 && decoder->disp) disp_free_buffers(decoder->disp,decoder->num_outBuf);
92 if (decoder->status) dce_free(decoder->status);
93 if (decoder->params) dce_free(decoder->params);
94 if (decoder->dynParams) dce_free(decoder->dynParams);
95 if (decoder->inBufs) {
96 dce_buf_unlock(1, &(decoder->inBufs->descs[0].buf));
97 close(decoder->inBufs->descs[0].buf);
98 dce_free(decoder->inBufs);
99 }
100 if (decoder->outBufs) dce_free(decoder->outBufs);
101 if (decoder->inArgs) dce_free(decoder->inArgs);
102 if (decoder->outArgs) dce_free(decoder->outArgs);
103 if (decoder->codec) VIDDEC3_delete(decoder->codec);
104 if (decoder->engine) Engine_close(decoder->engine);
105 if (decoder->input_bo) omap_bo_del(decoder->input_bo);
106 if (decoder->outBuf_fd) free(decoder->outBuf_fd);
107 if(inloop < 2) {
108 if (dev) dce_deinit(dev);
109 if (decoder->demux) demux_deinit(decoder->demux);
110 if (decoder->disp) disp_close(decoder->disp);
111 if(decoder) free(decoder);
112 }
113 }
115 static struct decoder *
116 decoder_open(int argc, char **argv)
117 {
118 static struct decoder *decoder = NULL;
119 char *infile = NULL;
120 int i;
121 static int width, height, padded_width, padded_height;
122 Engine_Error ec;
123 XDAS_Int32 err;
125 if(inloop < 2) {
126 decoder = calloc(1, sizeof(*decoder));
127 if (!decoder)
128 return NULL;
130 MSG("%p: Opening Display..", decoder);
131 decoder->disp = disp_open(argc, argv);
132 if (!decoder->disp)
133 goto usage;
135 /* loop thru args, find input file.. */
136 for (i = 1; i < argc; i++) {
137 int fd;
138 if (!argv[i]) {
139 continue;
140 }
141 fd = open(argv[i], 0);
142 if (fd > 0) {
143 infile = argv[i];
144 argv[i] = NULL;
145 close(fd);
146 break;
147 }
148 break;
149 }
150 if (check_args(argc, argv) || !infile)
151 goto usage;
152 MSG("%p: Opening Demuxer..", decoder);
153 decoder->demux = demux_init(infile, &width, &height);
154 if (!decoder->demux) {
155 ERROR("%p: could not open demuxer", decoder);
156 goto fail;
157 }
158 MSG("%p: infile=%s, width=%d, height=%d", decoder, infile, width, height);
160 /* calculate output buffer parameters: */
161 width = ALIGN2 (width, 4); /* round up to macroblocks */
162 height = ALIGN2 (height, 4); /* round up to macroblocks */
163 if (decoder->demux->cc->codec_id == CODEC_ID_MPEG2VIDEO) {
164 padded_width = width;
165 padded_height= height;
166 }
167 else {
168 padded_width = ALIGN2 (width + (2*PADX), 7);
169 padded_height = height + 4*PADY;
170 }
171 decoder->num_outBuf = MIN(16, 32768 / ((width/16) * (height/16))) + 3;
172 decoder->padded_width = padded_width;
173 decoder->padded_height = padded_height;
174 MSG("%p: padded_width=%d, padded_height=%d, num_buffers=%d",
175 decoder, padded_width, padded_height, decoder->num_outBuf);
176 dce_set_fd(decoder->disp->fd);
177 dev = dce_init();
178 if(dev == NULL) {
179 ERROR("%p: dce init failed", dev);
180 goto fail;
181 }
182 decoder->framebuf = disp_get_fb(decoder->disp);
183 if (! disp_get_vid_buffers(decoder->disp, decoder->num_outBuf,
184 FOURCC_STR("NV12"), decoder->padded_width, decoder->padded_height)) {
185 ERROR("%p: could not allocate buffers", decoder);
186 goto fail;
187 }
188 if(inloop) inloop = 2; /*Don't bother about looping if not asked to*/
189 }
191 if (!decoder->disp->multiplanar) {
192 decoder->uv_offset = padded_width * padded_height;
193 decoder->outBuf_fd = malloc(sizeof(int)*decoder->num_outBuf);
194 MSG("%p: uv_offset=%d", decoder, decoder->uv_offset);
195 }
196 else{
197 decoder->outBuf_fd = malloc(sizeof(int)*(decoder->num_outBuf*2));
198 }
200 decoder->input_sz = width * height;
201 decoder->input_bo = omap_bo_new(decoder->disp->dev,
202 decoder->input_sz, OMAP_BO_WC);
203 decoder->input = omap_bo_map(decoder->input_bo);
207 MSG("%p: Opening Engine..", decoder);
208 decoder->engine = Engine_open("ivahd_vidsvr", NULL, &ec);
209 if (!decoder->engine) {
210 ERROR("%p: could not open engine", decoder);
211 goto fail;
212 }
214 decoder->params = dce_alloc(sizeof(IVIDDEC3_Params));
215 decoder->params->size = sizeof(IVIDDEC3_Params);
217 decoder->params->maxWidth = width;
218 decoder->params->maxHeight = height;
219 decoder->params->maxFrameRate = 30000;
220 decoder->params->maxBitRate = 10000000;
221 decoder->params->dataEndianness = XDM_BYTE;
222 decoder->params->forceChromaFormat= XDM_YUV_420SP;
223 decoder->params->operatingMode = IVIDEO_DECODE_ONLY;
224 decoder->params->displayDelay = IVIDDEC3_DISPLAY_DELAY_AUTO;
225 decoder->params->displayBufsMode = IVIDDEC3_DISPLAYBUFS_EMBEDDED;
226 MSG("displayBufsMode: %d", decoder->params->displayBufsMode);
227 decoder->params->inputDataMode = IVIDEO_ENTIREFRAME;
228 decoder->params->metadataType[0] = IVIDEO_METADATAPLANE_NONE;
229 decoder->params->metadataType[1] = IVIDEO_METADATAPLANE_NONE;
230 decoder->params->metadataType[2] = IVIDEO_METADATAPLANE_NONE;
231 decoder->params->numInputDataUnits= 0;
232 decoder->params->outputDataMode = IVIDEO_ENTIREFRAME;
233 decoder->params->numOutputDataUnits = 0;
234 decoder->params->errorInfoMode = IVIDEO_ERRORINFO_OFF;
236 if (decoder->demux->cc->codec_id == CODEC_ID_MPEG2VIDEO) {
237 decoder->codec = VIDDEC3_create(decoder->engine,
238 "ivahd_mpeg2vdec", decoder->params);
239 }
240 else if (decoder->demux->cc->codec_id == CODEC_ID_H264) {
241 decoder->codec = VIDDEC3_create(decoder->engine,
242 "ivahd_h264dec", decoder->params);
243 }
244 else if (decoder->demux->cc->codec_id == CODEC_ID_MPEG4) {
245 first_in_buff = 1;
246 decoder->codec = VIDDEC3_create(decoder->engine,
247 "ivahd_mpeg4dec", decoder->params);
248 }
250 if (!decoder->codec) {
251 ERROR("%p: could not create codec", decoder);
252 goto fail;
253 }
255 decoder->dynParams = dce_alloc(sizeof(IVIDDEC3_DynamicParams));
256 decoder->dynParams->size = sizeof(IVIDDEC3_DynamicParams);
258 decoder->dynParams->decodeHeader = XDM_DECODE_AU;
260 /*Not Supported: Set default*/
261 decoder->dynParams->displayWidth = 0;
262 decoder->dynParams->frameSkipMode = IVIDEO_NO_SKIP;
263 decoder->dynParams->newFrameFlag = XDAS_TRUE;
265 decoder->status = dce_alloc(sizeof(IVIDDEC3_Status));
266 decoder->status->size = sizeof(IVIDDEC3_Status);
268 err = VIDDEC3_control(decoder->codec, XDM_SETPARAMS,
269 decoder->dynParams, decoder->status);
270 if (err) {
271 ERROR("%p: fail: %d", decoder, err);
272 goto fail;
273 }
275 /* not entirely sure why we need to call this here.. just copying omx.. */
276 err = VIDDEC3_control(decoder->codec, XDM_GETBUFINFO,
277 decoder->dynParams, decoder->status);
278 if (err) {
279 ERROR("%p: fail: %d", decoder, err);
280 goto fail;
281 }
283 decoder->inBufs = dce_alloc(sizeof(XDM2_BufDesc));
284 decoder->inBufs->numBufs = 1;
285 decoder->inBufs->descs[0].buf = (XDAS_Int8 *)omap_bo_dmabuf(decoder->input_bo);
286 dce_buf_lock(1, &(decoder->inBufs->descs[0].buf));
287 decoder->inBufs->descs[0].bufSize.bytes = omap_bo_size(decoder->input_bo);
288 decoder->inBufs->descs[0].memType = XDM_MEMTYPE_RAW;
291 decoder->outBufs = dce_alloc(sizeof(XDM2_BufDesc));
292 decoder->outBufs->numBufs = 2;
293 decoder->outBufs->descs[0].memType = XDM_MEMTYPE_RAW;
294 decoder->outBufs->descs[1].memType = XDM_MEMTYPE_RAW;
296 decoder->inArgs = dce_alloc(sizeof(IVIDDEC3_InArgs));
297 decoder->inArgs->size = sizeof(IVIDDEC3_InArgs);
299 decoder->outArgs = dce_alloc(sizeof(IVIDDEC3_OutArgs));
300 decoder->outArgs->size = sizeof(IVIDDEC3_OutArgs);
302 decoder->tdisp = mark(NULL);
304 return decoder;
306 usage:
307 usage(argv[0]);
308 fail:
309 if(inloop) inloop = 1; /*Error case: delete everything*/
310 if (decoder)
311 decoder_close(decoder);
312 return NULL;
313 }
315 static int
316 decoder_process(struct decoder *decoder)
317 {
318 XDM2_BufDesc *inBufs = decoder->inBufs;
319 XDM2_BufDesc *outBufs = decoder->outBufs;
320 VIDDEC3_InArgs *inArgs = decoder->inArgs;
321 VIDDEC3_OutArgs *outArgs = decoder->outArgs;
322 struct buffer *buf;
323 int freeBufCount =0;
324 int i, n;
325 XDAS_Int32 err;
326 int eof = 0; /* end of file flag */
329 /* demux; in loop mode, we can do two tries at the end of the stream. */
330 for (i = 0; i < 2; i++) {
331 n = demux_read(decoder->demux, decoder->input, decoder->input_sz);
332 if (n) {
333 buf = disp_get_vid_buffer(decoder->disp);
334 if (!buf) {
335 ERROR("%p: fail: out of buffers", decoder);
336 return -1;
337 }
338 inBufs->descs[0].bufSize.bytes = n;
339 inArgs->numBytes = n;
340 DBG("%p: push: %d bytes (%p)", decoder, n, buf);
341 } else {
342 /* end of input.. do we need to flush? */
343 MSG("%p: end of input", decoder);
345 /* In loop mode: rewind and retry once. */
346 if (loop && i == 0) {
347 int err = demux_rewind(decoder->demux);
348 if (err < 0) {
349 ERROR("%p: demux_rewind returned error: %d", decoder, err);
350 return -1;
351 }
352 MSG("%p: rewound.", decoder);
353 if(!inloop) continue;
354 }
355 eof = 1; /* set the flag for end of file to 1 */
356 /* Control call call with XDM_FLUSH command */
357 err = VIDDEC3_control(decoder->codec, XDM_FLUSH,
358 decoder->dynParams, decoder->status);
359 inBufs->numBufs = 0;
360 outBufs->numBufs = 0;
361 inArgs->inputID = 0;
362 }
363 break;
364 }
366 /*set the parameters if it is not the end of file */
367 if (!eof) {
368 inArgs->inputID = (XDAS_Int32)buf;
369 outBufs->descs[0].buf = buf->fd[0];
370 outBufs->descs[1].buf = (buf->multiplanar) ?buf->fd[1]:(XDAS_Int8 *)((outBufs->descs[0].buf));
373 if(buf->multiplanar){
374 decoder->outBuf_fd[0] = buf->fd[0];
375 decoder->outBuf_fd[1] = buf->fd[1];
376 dce_buf_lock(2,decoder->outBuf_fd);
377 }
378 else{
379 decoder->outBuf_fd[0] = buf->fd[0];
380 dce_buf_lock(1,decoder->outBuf_fd);
381 }
382 decoder->outBufs->descs[0].bufSize.bytes =decoder->padded_width*decoder->padded_height;
383 decoder->outBufs->descs[1].bufSize.bytes = decoder->padded_width* (decoder->padded_height/2);
384 }
386 do {
387 if (no_process) {
388 /* Do not process. This is for benchmarking. We need to "fake"
389 * the outArgs. */
390 outArgs->outputID[0] = 0;
391 outArgs->freeBufID[0] = 0;
392 if(!eof) {
393 outArgs->outputID[0] = buf;
394 outArgs->freeBufID[0] = buf;
395 }
396 outArgs->outputID[1] = NULL;
397 outArgs->freeBufID[1] = NULL;
398 outArgs->outBufsInUseFlag = 0;
400 } else {
401 suseconds_t tproc;
402 tproc = mark(NULL);
403 err = VIDDEC3_process(decoder->codec, inBufs, outBufs, inArgs, outArgs);
404 DBG("%p: processed returned in: %ldus", decoder, (long int)mark(&tproc));
405 if (err) {
406 ERROR("%p: process returned error: %d", decoder, err);
407 ERROR("%p: extendedError: %08x", decoder, outArgs->extendedError);
408 if (XDM_ISFATALERROR(outArgs->extendedError))
409 return -1;
410 }
411 }
413 for (i = 0; outArgs->outputID[i]; i++) {
414 /* calculate offset to region of interest */
415 XDM_Rect *r = &(outArgs->displayBufs.bufDesc[0].activeFrameRegion);
417 /* get the output buffer and write it to file */
418 buf = (struct buffer *)outArgs->outputID[i];
419 if(!no_process)
420 disp_post_vid_buffer(decoder->disp, buf,
421 r->topLeft.x, r->topLeft.y,
422 r->bottomRight.x - r->topLeft.x,
423 r->bottomRight.y - r->topLeft.y);
424 }
426 for (i = 0; outArgs->freeBufID[i]; i++) {
427 buf = (struct buffer *)outArgs->freeBufID[i];
428 disp_put_vid_buffer(decoder->disp, buf);
430 if(buf->multiplanar){
431 decoder->outBuf_fd[freeBufCount++] = buf->fd[0];
432 decoder->outBuf_fd[freeBufCount++] = buf->fd[1];
433 }
434 else{
435 decoder->outBuf_fd[freeBufCount++] = buf->fd[0];
436 DBG("FreeBufID: %p fd:%d\n", outArgs->freeBufID[i], buf->fd[0]);
437 }
438 }
440 if(freeBufCount){
441 if(!eof)dce_buf_unlock(freeBufCount,decoder->outBuf_fd);
442 freeBufCount =0;
443 }
444 if (outArgs->outBufsInUseFlag) {
445 MSG("%p: TODO... outBufsInUseFlag", decoder); // XXX
446 }
447 } while ((err == 0) && eof && !no_process);
449 return (inBufs->numBufs > 0) ? 0 : -1;
450 }
452 void *decode_stream(void *decoderHandle)
453 {
454 int ret = 0;
455 struct decoder *decoder = (struct decoder*)decoderHandle;
456 int n = 0;
457 if(!decoder) goto exit;
459 while((ret = decoder_process(decoder)) == 0);
460 if((ret != -1 && ret != 0) && inloop) inloop = 1; /*Assuming Good case. Otherwise logic gets messy*/
461 decoder_close(decoder);
462 exit:
463 return NULL;
464 }
466 int
467 main(int argc, char **argv)
468 {
469 struct decoder *decoders[8] = {};
470 int i, first = 0, ndecoders = 0;
472 for (i = 1; i < argc; i++) {
473 if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
474 usage(argv[0]);
475 exit(0);
477 } else if (!strcmp(argv[i], "--loop")) {
478 loop = 1;
479 argv[i] = NULL;
481 } else if (!strcmp(argv[i], "--no-process")) {
482 no_process = 1;
483 argv[i] = NULL;
485 } else if (!strcmp(argv[i], "--inloop")) {
486 inloop = 1; // this means inloop is detected
487 DBG("detected inloop = %d\n", inloop);
488 loop = 1; //we want rewind as well
489 argv[i] = NULL;
490 } else if (!strcmp(argv[i], "--")) {
491 argv[first] = argv[0];
492 decoders[ndecoders++] = decoder_open(i - first, &argv[first]);
493 first = i;
494 }
495 }
497 argv[first] = argv[0];
498 argc = i - first;
500 if(ndecoders) decoders[ndecoders++] = decoder_open(argc ,&argv[first]);
502 if (ndecoders > 1) {
503 pthread_t threadIds[8];
505 for (i = 0; i < ndecoders; i++) {
506 if (decoders[i]) {
507 int ret = pthread_create(&threadIds[i], NULL,
508 &decode_stream, decoders[i]);
509 if (ret != 0)
510 ERROR("%p: creation of pthread, error: %d",
511 decoders[i], ret);
512 }
513 }
515 for (i = 0; i < ndecoders; i++) {
516 pthread_join(threadIds[i], NULL);
517 }
518 }
519 else {
520 int itr = 0;
521 do {
522 decoders[0] = decoder_open(argc, &argv[first]);
523 decode_stream(decoders[0]);
524 MSG("=================Iteration %d complete =============== %d\n", ++itr);
525 }while(inloop);
526 }
528 return 0;
529 }