-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhdhome_run_mycodec.c
507 lines (472 loc) · 12.4 KB
/
hdhome_run_mycodec.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
/**
* mycodec calls
*
* Copyright 2018 Jay Sorg <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <a52dec/a52.h>
#include <mpeg2dec/mpeg2.h>
#include "hdhome_run_codec.h"
#include "hdhome_run_log.h"
struct mycodec_audio
{
a52_state_t* state;
sample_t* samples;
int frame_size;
int flags;
int sample_rate;
int channels;
int bit_rate;
int cdata_bytes;;
uint8_t* cdata;
};
struct mycodec_video
{
mpeg2dec_t* dec;
int width;
int height;
int got_frame;
int pad0;
void* ybuf;
void* ubuf;
void* vbuf;
};
/*****************************************************************************/
int
hdhome_run_codec_init(void)
{
return 0;
}
/*****************************************************************************/
int
hdhome_run_codec_audio_create(void** obj, int codec_id)
{
struct mycodec_audio* self;
if (obj == NULL)
{
return 1;
}
if (codec_id != AUDIO_CODEC_ID_AC3)
{
return 2;
}
self = (struct mycodec_audio*)calloc(sizeof(struct mycodec_audio), 1);
if (self == NULL)
{
return 3;
}
self->state = a52_init(0);
if (self->state == NULL)
{
free(self);
return 4;
}
self->samples = a52_samples(self->state);
if (self->samples == NULL)
{
a52_free(self->state);
free(self);
return 5;
}
*obj = self;
return 0;
}
/*****************************************************************************/
int
hdhome_run_codec_audio_delete(void* obj)
{
struct mycodec_audio* self;
if (obj == NULL)
{
return 0;
}
self = (struct mycodec_audio*)obj;
a52_free(self->state);
free(self->cdata);
free(self);
return 0;
}
static const int g_ac3_channels[8] = { 2, 1, 2, 3, 3, 4, 4, 5 };
/**** the following two functions comes from a52dec */
/*****************************************************************************/
static int
blah(int32_t i)
{
if (i > 0x43c07fff)
{
return 32767;
}
else if (i < 0x43bf8000)
{
return -32768;
}
return i - 0x43c00000;
}
/*****************************************************************************/
static void
float_to_short(float* in_float, int16_t* out_i16, int nchannels)
{
int i;
int j;
int c;
int32_t* f;
f = (int32_t*)in_float; /* XXX assumes IEEE float format */
j = 0;
nchannels *= 256;
for (i = 0; i < 256; i++)
{
for (c = 0; c < nchannels; c += 256)
{
out_i16[j++] = blah(f[i + c]);
}
}
}
/*****************************************************************************/
int
hdhome_run_codec_audio_decode(void* obj, void* cdata, int cdata_bytes,
int* cdata_bytes_processed, int* decoded)
{
struct mycodec_audio* self;
int flags;
int sample_rate;
int bit_rate;
int len;
float level;
LOGLN(10, (0, LOGF, LOGP));
LOGLN(10, (0, LOGF "cdata_bytes %d", LOGP, cdata_bytes));
*decoded = 0;
*cdata_bytes_processed = 0;
self = (struct mycodec_audio*)obj;
if (self->frame_size == 0)
{
if (cdata_bytes >= 7)
{
/* lookign for header */
flags = 0;
sample_rate = 0;
bit_rate = 0;
len = a52_syncinfo((uint8_t*)cdata, &flags, &sample_rate, &bit_rate);
if (len == 0)
{
return 1;
}
else
{
self->flags = flags;
self->frame_size = len;
self->sample_rate = sample_rate;
self->channels = g_ac3_channels[self->flags & 7];
if (self->flags & A52_LFE)
{
self->channels++;
}
self->bit_rate = bit_rate;
LOGLN(0, (0, LOGF "frame_size %d "
"channels %d sample_rate %d", LOGP,
self->frame_size, self->channels, self->sample_rate));
self->cdata = (uint8_t*)malloc(len);
if (self->cdata == NULL)
{
return 2;
}
self->cdata_bytes = 0;
return 0;
}
}
return 3;
}
len = self->frame_size - self->cdata_bytes;
if (len > cdata_bytes)
{
len = cdata_bytes;
}
if (len < 1)
{
return 4;
}
memcpy(self->cdata + self->cdata_bytes, cdata, len);
self->cdata_bytes += len;
*cdata_bytes_processed = len;
if (self->cdata_bytes >= self->frame_size)
{
self->cdata_bytes = 0;
flags = self->flags;
if (self->channels == 1)
{
flags = A52_MONO;
}
else if (self->channels == 2)
{
flags = A52_STEREO;
}
else
{
flags |= A52_ADJUST_LEVEL;
}
level = 1;
if (a52_frame(self->state, self->cdata, &flags, &level, 384))
{
return 5;
}
*decoded = 1;
}
return 0;
}
/*****************************************************************************/
int
hdhome_run_codec_audio_get_frame_info(void* obj, int* channels, int* format,
int* bytes)
{
struct mycodec_audio* self;
self = (struct mycodec_audio*)obj;
*channels = self->channels;
*format = 0;
*bytes = 6 * 256 * self->channels * 2;
return 0;
}
/*****************************************************************************/
int
hdhome_run_codec_audio_get_frame_data(void* obj, void* data, int data_bytes)
{
struct mycodec_audio* self;
int index;
short* out_samples;
self = (struct mycodec_audio*)obj;
out_samples = (short*)data;
for (index = 0; index < 6; index++)
{
if (a52_block(self->state))
{
return 1;
}
float_to_short(self->samples,
out_samples + index * 256 * self->channels,
self->channels);
}
return 0;
}
/*****************************************************************************/
int
hdhome_run_codec_video_create(void** obj, int codec_id)
{
struct mycodec_video* self;
if (obj == NULL)
{
return 1;
}
if (codec_id != VIDEO_CODEC_ID_MPEG2)
{
return 2;
}
self = (struct mycodec_video*)calloc(sizeof(struct mycodec_video), 1);
if (self == NULL)
{
return 3;
}
self->dec = mpeg2_init();
if (self->dec == NULL)
{
free(self);
return 4;
}
*obj = self;
return 0;
}
/*****************************************************************************/
int
hdhome_run_codec_video_delete(void* obj)
{
struct mycodec_video* self;
if (obj == NULL)
{
return 0;
}
self = (struct mycodec_video*)obj;
free(self->ybuf);
free(self->ubuf);
free(self->vbuf);
mpeg2_close(self->dec);
free(self);
return 0;
}
/*****************************************************************************/
static int
decode_mpeg2_frame(struct mycodec_video* self, const mpeg2_info_t* info)
{
int ybytes;
int uvbytes;
int lwidth;
int lheight;
if ((info->sequence != NULL) && (info->display_fbuf != NULL))
{
LOGLN(10, (0, LOGF "width %d height %d frame_period %d", LOGP,
info->sequence->width, info->sequence->height,
info->sequence->frame_period));
lwidth = info->sequence->width;
lheight = info->sequence->height;
if ((lwidth < 16) || (lwidth > 4096) ||
(lheight < 16) || (lheight > 4096))
{
return 1;
}
ybytes = lwidth * lheight;
uvbytes = ybytes / 4;
if ((self->width != lwidth) || (self->height != lheight))
{
self->width = lwidth;
self->height = lheight;
free(self->ybuf);
free(self->ubuf);
free(self->vbuf);
self->ybuf = malloc(ybytes);
self->ubuf = malloc(uvbytes);
self->vbuf = malloc(uvbytes);
if ((self->ybuf == NULL) || (self->ubuf == NULL) ||
(self->vbuf == NULL))
{
self->width = 0;
self->height = 0;
return 2;
}
}
memcpy(self->ybuf, info->display_fbuf->buf[0], ybytes);
memcpy(self->ubuf, info->display_fbuf->buf[1], uvbytes);
memcpy(self->vbuf, info->display_fbuf->buf[2], uvbytes);
self->got_frame = 1;
}
return 0;
}
/*****************************************************************************/
static int
decode_mpeg2(struct mycodec_video* self, uint8_t* start, uint8_t* end)
{
const mpeg2_info_t* info;
mpeg2_state_t state;
int error;
LOGLN(10, (0, LOGF, LOGP));
error = 0;
mpeg2_buffer(self->dec, start, end);
info = mpeg2_info(self->dec);
while (error == 0)
{
state = mpeg2_parse(self->dec);
LOGLN(10, (0, LOGF "state %d", LOGP, state));
switch (state)
{
case STATE_BUFFER:
LOGLN(10, (0, LOGF "STATE_BUFFER", LOGP));
return 0;
case STATE_SEQUENCE:
LOGLN(10, (0, LOGF "STATE_SEQUENCE", LOGP));
break;
case STATE_PICTURE:
LOGLN(10, (0, LOGF "STATE_PICTURE", LOGP));
break;
case STATE_SLICE:
case STATE_END:
case STATE_INVALID_END:
error = decode_mpeg2_frame(self, info);
break;
default:
break;
}
}
return error;
}
/*****************************************************************************/
int
hdhome_run_codec_video_decode(void* obj, void* cdata, int cdata_bytes,
int* cdata_bytes_processed, int* decoded)
{
struct mycodec_video* self;
uint8_t* start;
uint8_t* end;
int error;
LOGLN(10, (0, LOGF, LOGP));
LOGLN(10, (0, LOGF "cdata_bytes %d", LOGP, cdata_bytes));
self = (struct mycodec_video*)obj;
start = (uint8_t*)cdata;
end = start + cdata_bytes;
error = decode_mpeg2(self, start, end);
if (error == 0)
{
*decoded = self->got_frame;
*cdata_bytes_processed = cdata_bytes;
}
self->got_frame = 0;
return error;
}
/*****************************************************************************/
int
hdhome_run_codec_video_get_frame_info(void* obj, int* width, int* height,
int* format, int* bytes)
{
struct mycodec_video* self;
int lwidth;
int lheight;
self = (struct mycodec_video*)obj;
lwidth = self->width;
lheight = self->height;
if ((lwidth < 1) || (lheight < 1))
{
return 1;
}
LOGLN(10, (0, LOGF "width %d height %d", LOGP, lwidth, lheight));
*width = lwidth;
*height = lheight;
*format = 0; /* I420 */
*bytes = lwidth * lheight * 3 / 2;
return 0;
}
/*****************************************************************************/
int
hdhome_run_codec_video_get_frame_data(void* obj, void* data, int data_bytes)
{
struct mycodec_video* self;
uint8_t* dst8;
int lwidth;
int lheight;
int bytes;
self = (struct mycodec_video*)obj;
lwidth = self->width;
lheight = self->height;
dst8 = (uint8_t*)data;
bytes = lwidth * lheight;
if (bytes > data_bytes)
{
bytes = data_bytes;
}
memcpy(dst8, self->ybuf, bytes);
dst8 += bytes;
data_bytes -= bytes;
bytes = lwidth * lheight / 4;
if (bytes > data_bytes)
{
bytes = data_bytes;
}
memcpy(dst8, self->ubuf, bytes);
dst8 += bytes;
data_bytes -= bytes;
bytes = lwidth * lheight / 4;
if (bytes > data_bytes)
{
bytes = data_bytes;
}
memcpy(dst8, self->vbuf, bytes);
return 0;
}