-
Notifications
You must be signed in to change notification settings - Fork 1
/
dce.c
320 lines (264 loc) · 9.37 KB
/
dce.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
/*
Copyright (C) 2010 Mans Rullgard
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <stdint.h>
#include <stdlib.h>
#include <tilermem.h>
#include <memmgr.h>
#include <dce.h>
#include <xdc/std.h>
#include <ti/sdo/ce/Engine.h>
#include <ti/sdo/ce/video3/viddec3.h>
#include <libavcodec/avcodec.h>
#include "frame.h"
#include "codec.h"
#include "util.h"
#define PADX 32
#define PADY 24
static Engine_Handle engine;
static VIDDEC3_Handle codec;
static VIDDEC3_Params *params;
static VIDDEC3_DynamicParams *dyn_params;
static VIDDEC3_Status *status;
static XDM2_BufDesc *inbufs;
static XDM2_BufDesc *outbufs;
static VIDDEC3_InArgs *in_args;
static VIDDEC3_OutArgs *out_args;
static uint8_t *input_buf;
static uint32_t input_phys;
static int input_size;
static AVCodecContext *avc;
static AVBitStreamFilterContext *bsf;
static int alloc_input(int size)
{
MemAllocBlock mablk = { 0 };
uint8_t *buf;
if (size <= input_size)
return 0;
size = ALIGN(size * 5 / 4, 8192);
mablk.pixelFormat = PIXEL_FMT_PAGE;
mablk.dim.len = size;
buf = MemMgr_Alloc(&mablk, 1);
if (!buf)
return -1;
if (input_buf)
MemMgr_Free(input_buf);
input_buf = buf;
input_phys = TilerMem_VirtToPhys(input_buf);
input_size = size;
return 0;
}
static void dce_close(void)
{
if (codec) VIDDEC3_delete(codec); codec = NULL;
if (engine) Engine_close(engine); engine = NULL;
if (params) dce_free(params); params = NULL;
if (dyn_params) dce_free(dyn_params); dyn_params = NULL;
if (status) dce_free(status); status = NULL;
if (inbufs) dce_free(inbufs); inbufs = NULL;
if (outbufs) dce_free(outbufs); outbufs = NULL;
if (in_args) dce_free(in_args); in_args = NULL;
if (out_args) dce_free(out_args); out_args = NULL;
if (input_buf) MemMgr_Free(input_buf); input_buf = NULL;
if (bsf) av_bitstream_filter_close(bsf); bsf = NULL;
}
static int dce_open(const char *name, AVCodecContext *cc,
struct frame_format *ff)
{
Engine_Error ec;
XDAS_Int32 err;
if (cc->codec_id != CODEC_ID_H264) {
fprintf(stderr, "DCE: unsupported codec %d\n", cc->codec_id);
return -1;
}
if (cc->extradata && cc->extradata_size > 0 && cc->extradata[0] == 1) {
bsf = av_bitstream_filter_init("h264_mp4toannexb");
if (!bsf)
return -1;
avc = cc;
}
ff->width = ALIGN(cc->width + 2*PADX, 128);
ff->height = ALIGN(cc->height + 4*PADY, 16);
ff->disp_x = 0;
ff->disp_y = 0;
ff->disp_w = cc->width;
ff->disp_h = cc->height;
ff->pixfmt = PIX_FMT_NV12;
engine = Engine_open("ivahd_vidsvr", NULL, &ec);
if (!engine) {
fprintf(stderr, "Engine_open() failed\n");
goto err;
}
params = dce_alloc(sizeof(*params));
if (!params)
return -1;
params->size = sizeof(*params);
params->maxWidth = ALIGN(ff->disp_w, 16);
params->maxHeight = ALIGN(ff->disp_h, 16);
params->maxFrameRate = 30000;
params->maxBitRate = 10000000;
params->dataEndianness = XDM_BYTE;
params->forceChromaFormat = XDM_YUV_420SP;
params->operatingMode = IVIDEO_DECODE_ONLY;
params->displayDelay = IVIDDEC3_DISPLAY_DELAY_AUTO;
params->displayBufsMode = IVIDDEC3_DISPLAYBUFS_EMBEDDED;
params->inputDataMode = IVIDEO_ENTIREFRAME;
params->metadataType[0] = IVIDEO_METADATAPLANE_NONE;
params->metadataType[1] = IVIDEO_METADATAPLANE_NONE;
params->metadataType[2] = IVIDEO_METADATAPLANE_NONE;
params->numInputDataUnits = 0;
params->outputDataMode = IVIDEO_ENTIREFRAME;
params->numOutputDataUnits = 0;
params->errorInfoMode = IVIDEO_ERRORINFO_OFF;
codec = VIDDEC3_create(engine, "ivahd_h264dec", params);
if (!codec) {
fprintf(stderr, "VIDDEC3_create() failed\n");
goto err;
}
dyn_params = dce_alloc(sizeof(*dyn_params));
if (!dyn_params)
goto err;
dyn_params->size = sizeof(*dyn_params);
dyn_params->decodeHeader = XDM_DECODE_AU;
dyn_params->displayWidth = ff->width;
dyn_params->frameSkipMode = IVIDEO_NO_SKIP;
dyn_params->newFrameFlag = XDAS_TRUE;
status = dce_alloc(sizeof(*status));
if (!status)
goto err;
status->size = sizeof(*status);
err = VIDDEC3_control(codec, XDM_SETPARAMS, dyn_params, status);
if (err) {
fprintf(stderr, "VIDDEC3_control(XDM_SETPARAMS) failed %d\n", err);
goto err;
}
err = VIDDEC3_control(codec, XDM_GETBUFINFO, dyn_params, status);
if (err) {
fprintf(stderr, "VIDDEC3_control(XDM_GETBUFINFO) failed %d\n", err);
goto err;
}
if (alloc_input(ff->disp_w * ff->disp_h))
goto err;
inbufs = dce_alloc(sizeof(*inbufs));
if (!inbufs)
goto err;
outbufs = dce_alloc(sizeof(*outbufs));
if (!outbufs)
goto err;
in_args = dce_alloc(sizeof(*in_args));
if (!in_args)
goto err;
in_args->size = sizeof(*in_args);
out_args = dce_alloc(sizeof(*out_args));
if (!out_args)
goto err;
out_args->size = sizeof(*out_args);
return 0;
err:
dce_close();
return -1;
}
static void setup_buf(XDM2_SingleBufDesc *desc,
unsigned long paddr, int w, int h)
{
XDAS_Int16 type = XDM_MEMTYPE_RAW;
if ((0x60000000 <= paddr) && (paddr < 0x68000000))
type = XDM_MEMTYPE_TILED8;
else if ((0x68000000 <= paddr) && (paddr < 0x70000000))
type = XDM_MEMTYPE_TILED16;
else if ((0x70000000 <= paddr) && (paddr < 0x78000000))
type = XDM_MEMTYPE_TILED32;
else if ((0x78000000 <= paddr) && (paddr < 0x80000000))
type = XDM_MEMTYPE_TILEDPAGE;
desc->memType = type;
desc->buf = (int8_t*)paddr;
if ((type == XDM_MEMTYPE_RAW) || (type == XDM_MEMTYPE_TILEDPAGE)) {
desc->bufSize.bytes = w * h;
} else {
desc->bufSize.tileMem.width = w;
desc->bufSize.tileMem.height = h;
}
}
static int dce_decode(AVPacket *p)
{
struct frame *f;
uint8_t *buf;
int bufsize;
int err;
int i;
if (bsf) {
if (av_bitstream_filter_filter(bsf, avc, NULL, &buf, &bufsize,
p->data, p->size, 0) < 0) {
fprintf(stderr, "DCE: bsf error\n");
return -1;
}
} else {
buf = p->data;
bufsize = p->size;
}
if (alloc_input(bufsize))
return -1;
memcpy(input_buf, buf, bufsize);
if (bsf)
av_free(buf);
f = ofbp_get_frame();
if (!f->phys[0]) {
f->phys[0] = (uint8_t*)TilerMem_VirtToPhys(f->virt[0]);
f->phys[1] = (uint8_t*)TilerMem_VirtToPhys(f->virt[1]);
}
in_args->inputID = (XDAS_Int32)f;
in_args->numBytes = bufsize;
inbufs->numBufs = 1;
inbufs->descs[0].memType = XDM_MEMTYPE_RAW;
inbufs->descs[0].buf = (int8_t*)input_phys;
inbufs->descs[0].bufSize.bytes = bufsize;
outbufs->numBufs = 2;
setup_buf(&outbufs->descs[0], (unsigned long)f->phys[0],
f->ff->width, f->ff->height);
setup_buf(&outbufs->descs[1], (unsigned long)f->phys[1],
f->ff->width, f->ff->height/2);
err = VIDDEC3_process(codec, inbufs, outbufs, in_args, out_args);
if (err) {
fprintf(stderr, "VIDDEC3_process() error %d %08x\n", err,
out_args->extendedError);
/* for non-fatal errors, keep going.. a non-fatal error could
* just indicate an error in the input stream which the codec
* was able to conceal
*/
if (XDM_ISFATALERROR(out_args->extendedError))
return -1;
}
for (i = 0; i < out_args->outputID[i]; i++) {
XDM_Rect *r = &out_args->displayBufs.bufDesc[0].activeFrameRegion;
f->x = r->topLeft.x;
f->y = r->topLeft.y;
ofbp_post_frame((struct frame *)out_args->outputID[i]);
}
for (i = 0; out_args->freeBufID[i]; i++)
ofbp_put_frame((struct frame *)out_args->freeBufID[i]);
return 0;
}
CODEC(avcodec) = {
.name = "dce",
.flags = OFBP_PHYS_MEM,
.open = dce_open,
.decode = dce_decode,
.close = dce_close,
};