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;
66 /* When true, loop at end of playback. */
67 static int loop = 0;
69 static void
70 usage(char *name)
71 {
72 MSG("Usage: %s [OPTIONS] INFILE", name);
73 MSG("Test of viddec3 decoder.");
74 MSG("");
75 MSG("viddec3test options:");
76 MSG("\t-h, --help: Print this help and exit.");
77 MSG("\t--loop\tRestart playback at end of stream.");
78 MSG("\t--no-process\tDo not actually call VIDDEC3_process method. For benchmarking.");
79 MSG("");
80 disp_usage();
81 }
83 static void
84 decoder_close(struct decoder *decoder)
85 {
86 /* free output buffers allocated by display */
87 disp_free_buffers(decoder->disp,decoder->num_outBuf);
89 if (decoder->status) dce_free(decoder->status);
90 if (decoder->params) dce_free(decoder->params);
91 if (decoder->dynParams) dce_free(decoder->dynParams);
92 if (decoder->inBufs) {
93 dce_buf_unlock(1, &(decoder->inBufs->descs[0].buf));
94 close(decoder->inBufs->descs[0].buf);
95 dce_free(decoder->inBufs);
96 }
97 if (decoder->outBufs) dce_free(decoder->outBufs);
98 if (decoder->inArgs) dce_free(decoder->inArgs);
99 if (decoder->outArgs) dce_free(decoder->outArgs);
100 if (decoder->codec) VIDDEC3_delete(decoder->codec);
101 if (decoder->engine) Engine_close(decoder->engine);
102 if (dev) dce_deinit(dev);
103 if (decoder->input_bo) omap_bo_del(decoder->input_bo);
104 if (decoder->demux) demux_deinit(decoder->demux);
105 if (decoder->disp) disp_close(decoder->disp);
106 if (decoder->outBuf_fd) free(decoder->outBuf_fd);
107 free(decoder);
108 }
110 static struct decoder *
111 decoder_open(int argc, char **argv)
112 {
113 struct decoder *decoder;
114 char *infile = NULL;
115 int i;
116 int width, height, padded_width, padded_height;
117 Engine_Error ec;
118 XDAS_Int32 err;
120 decoder = calloc(1, sizeof(*decoder));
121 if (!decoder)
122 return NULL;
124 MSG("%p: Opening Display..", decoder);
125 decoder->disp = disp_open(argc, argv);
126 if (!decoder->disp)
127 goto usage;
129 /* loop thru args, find input file.. */
130 for (i = 1; i < argc; i++) {
131 int fd;
132 if (!argv[i]) {
133 continue;
134 }
135 fd = open(argv[i], 0);
136 if (fd > 0) {
137 infile = argv[i];
138 argv[i] = NULL;
139 close(fd);
140 break;
141 }
142 break;
143 }
145 if (check_args(argc, argv) || !infile)
146 goto usage;
148 MSG("%p: Opening Demuxer..", decoder);
149 decoder->demux = demux_init(infile, &width, &height);
150 if (!decoder->demux) {
151 ERROR("%p: could not open demuxer", decoder);
152 goto fail;
153 }
155 MSG("%p: infile=%s, width=%d, height=%d", decoder, infile, width, height);
157 /* calculate output buffer parameters: */
158 width = ALIGN2 (width, 4); /* round up to macroblocks */
159 height = ALIGN2 (height, 4); /* round up to macroblocks */
161 if (decoder->demux->cc->codec_id == CODEC_ID_MPEG2VIDEO) {
162 padded_width = width;
163 padded_height= height;
164 }
165 else {
166 padded_width = ALIGN2 (width + (2*PADX), 7);
167 padded_height = height + 4*PADY;
168 }
170 decoder->num_outBuf = MIN(16, 32768 / ((width/16) * (height/16))) + 3;
171 decoder->padded_width = padded_width;
172 decoder->padded_height = padded_height;
173 MSG("%p: padded_width=%d, padded_height=%d, num_buffers=%d",
174 decoder, padded_width, padded_height, decoder->num_outBuf);
176 if (!decoder->disp->multiplanar) {
177 decoder->uv_offset = padded_width * padded_height;
178 decoder->outBuf_fd = malloc(sizeof(int)*decoder->num_outBuf);
179 MSG("%p: uv_offset=%d", decoder, decoder->uv_offset);
180 }
181 else{
182 decoder->outBuf_fd = malloc(sizeof(int)*(decoder->num_outBuf*2));
183 }
185 decoder->input_sz = width * height;
186 decoder->input_bo = omap_bo_new(decoder->disp->dev,
187 decoder->input_sz, OMAP_BO_WC);
188 decoder->input = omap_bo_map(decoder->input_bo);
189 decoder->framebuf = disp_get_fb(decoder->disp);
191 if (! disp_get_vid_buffers(decoder->disp, decoder->num_outBuf,
192 FOURCC_STR("NV12"), padded_width, padded_height)) {
193 ERROR("%p: could not allocate buffers", decoder);
194 goto fail;
195 }
197 MSG("%p: Opening Engine..", decoder);
198 dce_set_fd(decoder->disp->fd);
199 dev = dce_init();
200 if(dev == NULL) {
201 ERROR("%p: dce init failed", dev);
202 goto fail;
203 }
204 decoder->engine = Engine_open("ivahd_vidsvr", NULL, &ec);
205 if (!decoder->engine) {
206 ERROR("%p: could not open engine", decoder);
207 goto fail;
208 }
210 decoder->params = dce_alloc(sizeof(IVIDDEC3_Params));
211 decoder->params->size = sizeof(IVIDDEC3_Params);
213 decoder->params->maxWidth = width;
214 decoder->params->maxHeight = height;
215 decoder->params->maxFrameRate = 30000;
216 decoder->params->maxBitRate = 10000000;
217 decoder->params->dataEndianness = XDM_BYTE;
218 decoder->params->forceChromaFormat= XDM_YUV_420SP;
219 decoder->params->operatingMode = IVIDEO_DECODE_ONLY;
220 decoder->params->displayDelay = IVIDDEC3_DISPLAY_DELAY_AUTO;
221 decoder->params->displayBufsMode = IVIDDEC3_DISPLAYBUFS_EMBEDDED;
222 MSG("displayBufsMode: %d", decoder->params->displayBufsMode);
223 decoder->params->inputDataMode = IVIDEO_ENTIREFRAME;
224 decoder->params->metadataType[0] = IVIDEO_METADATAPLANE_NONE;
225 decoder->params->metadataType[1] = IVIDEO_METADATAPLANE_NONE;
226 decoder->params->metadataType[2] = IVIDEO_METADATAPLANE_NONE;
227 decoder->params->numInputDataUnits= 0;
228 decoder->params->outputDataMode = IVIDEO_ENTIREFRAME;
229 decoder->params->numOutputDataUnits = 0;
230 decoder->params->errorInfoMode = IVIDEO_ERRORINFO_OFF;
232 if (decoder->demux->cc->codec_id == CODEC_ID_MPEG2VIDEO) {
233 decoder->codec = VIDDEC3_create(decoder->engine,
234 "ivahd_mpeg2vdec", decoder->params);
235 }
236 else if (decoder->demux->cc->codec_id == CODEC_ID_H264) {
237 decoder->codec = VIDDEC3_create(decoder->engine,
238 "ivahd_h264dec", decoder->params);
239 }
240 else if (decoder->demux->cc->codec_id == CODEC_ID_MPEG4) {
241 first_in_buff = 1;
242 decoder->codec = VIDDEC3_create(decoder->engine,
243 "ivahd_mpeg4dec", decoder->params);
244 }
246 if (!decoder->codec) {
247 ERROR("%p: could not create codec", decoder);
248 goto fail;
249 }
251 decoder->dynParams = dce_alloc(sizeof(IVIDDEC3_DynamicParams));
252 decoder->dynParams->size = sizeof(IVIDDEC3_DynamicParams);
254 decoder->dynParams->decodeHeader = XDM_DECODE_AU;
256 /*Not Supported: Set default*/
257 decoder->dynParams->displayWidth = 0;
258 decoder->dynParams->frameSkipMode = IVIDEO_NO_SKIP;
259 decoder->dynParams->newFrameFlag = XDAS_TRUE;
261 decoder->status = dce_alloc(sizeof(IVIDDEC3_Status));
262 decoder->status->size = sizeof(IVIDDEC3_Status);
264 err = VIDDEC3_control(decoder->codec, XDM_SETPARAMS,
265 decoder->dynParams, decoder->status);
266 if (err) {
267 ERROR("%p: fail: %d", decoder, err);
268 goto fail;
269 }
271 /* not entirely sure why we need to call this here.. just copying omx.. */
272 err = VIDDEC3_control(decoder->codec, XDM_GETBUFINFO,
273 decoder->dynParams, decoder->status);
274 if (err) {
275 ERROR("%p: fail: %d", decoder, err);
276 goto fail;
277 }
279 decoder->inBufs = dce_alloc(sizeof(XDM2_BufDesc));
280 decoder->inBufs->numBufs = 1;
281 decoder->inBufs->descs[0].buf = (XDAS_Int8 *)omap_bo_dmabuf(decoder->input_bo);
282 dce_buf_lock(1, &(decoder->inBufs->descs[0].buf));
283 decoder->inBufs->descs[0].bufSize.bytes = omap_bo_size(decoder->input_bo);
284 decoder->inBufs->descs[0].memType = XDM_MEMTYPE_RAW;
287 decoder->outBufs = dce_alloc(sizeof(XDM2_BufDesc));
288 decoder->outBufs->numBufs = 2;
289 decoder->outBufs->descs[0].memType = XDM_MEMTYPE_RAW;
290 decoder->outBufs->descs[1].memType = XDM_MEMTYPE_RAW;
292 decoder->inArgs = dce_alloc(sizeof(IVIDDEC3_InArgs));
293 decoder->inArgs->size = sizeof(IVIDDEC3_InArgs);
295 decoder->outArgs = dce_alloc(sizeof(IVIDDEC3_OutArgs));
296 decoder->outArgs->size = sizeof(IVIDDEC3_OutArgs);
298 decoder->tdisp = mark(NULL);
300 return decoder;
302 usage:
303 usage(argv[0]);
304 fail:
305 if (decoder)
306 decoder_close(decoder);
307 return NULL;
308 }
310 static int
311 decoder_process(struct decoder *decoder)
312 {
313 XDM2_BufDesc *inBufs = decoder->inBufs;
314 XDM2_BufDesc *outBufs = decoder->outBufs;
315 VIDDEC3_InArgs *inArgs = decoder->inArgs;
316 VIDDEC3_OutArgs *outArgs = decoder->outArgs;
317 struct buffer *buf;
318 int freeBufCount =0;
319 int i, n;
320 XDAS_Int32 err;
321 int eof = 0; /* end of file flag */
323 buf = disp_get_vid_buffer(decoder->disp);
324 if (!buf) {
325 ERROR("%p: fail: out of buffers", decoder);
326 return -1;
327 }
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) {
334 inBufs->descs[0].bufSize.bytes = n;
335 inArgs->numBytes = n;
336 DBG("%p: push: %d bytes (%p)", decoder, n, buf);
337 } else {
338 /* end of input.. do we need to flush? */
339 MSG("%p: end of input", decoder);
341 /* In loop mode: rewind and retry once. */
342 if (loop && i == 0) {
343 int err = demux_rewind(decoder->demux);
344 if (err < 0) {
345 ERROR("%p: demux_rewind returned error: %d", decoder, err);
346 return -1;
347 }
348 MSG("%p: rewound.", decoder);
349 continue;
350 }
351 eof = 1; /* set the flag for end of file to 1 */
352 /* Control call call with XDM_FLUSH command */
353 err = VIDDEC3_control(decoder->codec, XDM_FLUSH,
354 decoder->dynParams, decoder->status);
355 inBufs->numBufs = 0;
356 outBufs->numBufs = 0;
357 inArgs->inputID = 0;
358 }
359 break;
360 }
362 /*set the parameters if it is not the end of file */
363 if (!eof) {
364 inArgs->inputID = (XDAS_Int32)buf;
365 outBufs->descs[0].buf = buf->fd[0];
366 outBufs->descs[1].buf = (buf->multiplanar) ?buf->fd[1]:(XDAS_Int8 *)((outBufs->descs[0].buf));
369 if(buf->multiplanar){
370 decoder->outBuf_fd[0] = buf->fd[0];
371 decoder->outBuf_fd[1] = buf->fd[1];
372 dce_buf_lock(2,decoder->outBuf_fd);
373 }
374 else{
375 decoder->outBuf_fd[0] = buf->fd[0];
376 dce_buf_lock(1,decoder->outBuf_fd);
377 }
378 decoder->outBufs->descs[0].bufSize.bytes =decoder->padded_width*decoder->padded_height;
379 decoder->outBufs->descs[1].bufSize.bytes = decoder->padded_width* (decoder->padded_height/2);
380 }
382 do {
383 if (no_process) {
384 /* Do not process. This is for benchmarking. We need to "fake"
385 * the outArgs. */
386 outArgs->outputID[0] = buf;
387 outArgs->outputID[1] = NULL;
388 outArgs->freeBufID[0] = buf;
389 outArgs->freeBufID[1] = NULL;
390 outArgs->outBufsInUseFlag = 0;
392 } else {
393 suseconds_t tproc;
394 tproc = mark(NULL);
395 err = VIDDEC3_process(decoder->codec, inBufs, outBufs, inArgs, outArgs);
396 DBG("%p: processed returned in: %ldus", decoder, (long int)mark(&tproc));
397 if (err) {
398 ERROR("%p: process returned error: %d", decoder, err);
399 ERROR("%p: extendedError: %08x", decoder, outArgs->extendedError);
400 if (XDM_ISFATALERROR(outArgs->extendedError))
401 return -1;
402 }
403 }
405 for (i = 0; outArgs->outputID[i]; i++) {
406 /* calculate offset to region of interest */
407 XDM_Rect *r = &(outArgs->displayBufs.bufDesc[0].activeFrameRegion);
409 /* get the output buffer and write it to file */
410 buf = (struct buffer *)outArgs->outputID[i];
411 disp_post_vid_buffer(decoder->disp, buf,
412 r->topLeft.x, r->topLeft.y,
413 r->bottomRight.x - r->topLeft.x,
414 r->bottomRight.y - r->topLeft.y);
415 }
417 for (i = 0; outArgs->freeBufID[i]; i++) {
418 buf = (struct buffer *)outArgs->freeBufID[i];
419 disp_put_vid_buffer(decoder->disp, buf);
421 if(buf->multiplanar){
422 decoder->outBuf_fd[freeBufCount++] = buf->fd[0];
423 decoder->outBuf_fd[freeBufCount++] = buf->fd[1];
424 }
425 else{
426 decoder->outBuf_fd[freeBufCount++] = buf->fd[0];
427 DBG("FreeBufID: %p fd:%d\n", outArgs->freeBufID[i], buf->fd[0]);
428 }
429 }
431 if(freeBufCount){
432 dce_buf_unlock(freeBufCount,decoder->outBuf_fd);
433 freeBufCount =0;
434 }
435 if (outArgs->outBufsInUseFlag) {
436 MSG("%p: TODO... outBufsInUseFlag", decoder); // XXX
437 }
438 } while ((err == 0) && eof);
440 return (inBufs->numBufs > 0) ? 0 : -1;
441 }
443 void *decode_stream(void *decoderHandle)
444 {
445 int n = 0;
446 struct decoder *decoders = (struct decoder*)decoderHandle;
448 do {
449 if (decoders) {
450 int ret = decoder_process(decoders);
451 if (ret) {
452 decoder_close(decoders);
453 decoders = NULL;
454 n = 0;
455 continue;
456 }
457 n++;
458 }
459 } while(n > 0);
461 return NULL;
462 }
464 int
465 main(int argc, char **argv)
466 {
467 struct decoder *decoders[8] = {};
468 int i, first = 0, ndecoders = 0;
470 for (i = 1; i < argc; i++) {
471 if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) {
472 usage(argv[0]);
473 exit(0);
475 } else if (!strcmp(argv[i], "--loop")) {
476 loop = 1;
477 argv[i] = NULL;
479 } else if (!strcmp(argv[i], "--no-process")) {
480 no_process = 1;
481 argv[i] = NULL;
483 } else if (!strcmp(argv[i], "--")) {
484 argv[first] = argv[0];
485 decoders[ndecoders++] = decoder_open(i - first, &argv[first]);
486 first = i;
487 }
488 }
490 argv[first] = argv[0];
492 decoders[ndecoders++] = decoder_open(i - first, &argv[first]);
494 if (ndecoders > 1) {
495 pthread_t threadIds[8];
497 for (i = 0; i < ndecoders; i++) {
498 if (decoders[i]) {
499 int ret = pthread_create(&threadIds[i], NULL,
500 &decode_stream, decoders[i]);
501 if (ret != 0)
502 ERROR("%p: creation of pthread, error: %d",
503 decoders[i], ret);
504 }
505 }
507 for (i = 0; i < ndecoders; i++) {
508 pthread_join(threadIds[i], NULL);
509 }
510 }
511 else {
512 decode_stream(decoders[0]);
513 }
515 return 0;
516 }