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
|
/*
* decoder.cpp: A program for the Video Disk Recorder
*
* See the README file for copyright information and how to reach the author.
*
*/
#include "decoder.h"
cMarkAdDecoder::cMarkAdDecoder(bool useH264, bool useMP2, bool hasAC3)
{
avcodec_init();
avcodec_register_all();
last_qscale_table=NULL;
cpu_set_t cpumask;
uint len = sizeof(cpumask);
int cpucount;
if (sched_getaffinity(0,len,&cpumask)<0)
{
cpucount=1;
}
else
{
cpucount=CPU_COUNT(&cpumask);
}
int ver = avcodec_version();
char libver[256];
snprintf(libver,sizeof(libver),"%i.%i.%i",ver >> 16 & 0xFF,ver >> 8 & 0xFF,ver & 0xFF);
isyslog("using libavcodec.so.%s with %i threads",libver,cpucount);
if (ver!=LIBAVCODEC_VERSION_INT)
{
esyslog("libavcodec header version %s",AV_STRINGIFY(LIBAVCODEC_VERSION));
esyslog("header and library mismatch, decoding disabled");
video_context=NULL;
ac3_context=NULL;
mp2_context=NULL;
audiobuf=NULL;
return;
}
if (((ver >> 16)<52) && (useH264))
{
esyslog("dont report bugs about H264, use libavcodec >= 52 instead!");
}
if (useMP2)
{
CodecID mp2_codecid=CODEC_ID_MP2;
AVCodec *mp2_codec= avcodec_find_decoder(mp2_codecid);
if (mp2_codec)
{
mp2_context = avcodec_alloc_context();
if (mp2_context)
{
mp2_context->thread_count=cpucount;
mp2_context->codec_id = mp2_codecid;
mp2_context->codec_type = CODEC_TYPE_AUDIO;
if (avcodec_open(mp2_context, mp2_codec) < 0)
{
esyslog("could not open codec for MP2");
av_free(mp2_context);
mp2_context=NULL;
}
}
else
{
esyslog("could not allocate mp2 context");
}
}
else
{
esyslog("codec for MP2 not found");
mp2_context=NULL;
}
}
else
{
mp2_context=NULL;
}
if (hasAC3)
{
CodecID ac3_codecid=CODEC_ID_AC3;
AVCodec *ac3_codec= avcodec_find_decoder(ac3_codecid);
if (ac3_codec)
{
ac3_context = avcodec_alloc_context();
if (ac3_context)
{
ac3_context->thread_count=cpucount;
ac3_context->codec_id = ac3_codecid;
ac3_context->codec_type = CODEC_TYPE_AUDIO;
if (avcodec_open(ac3_context, ac3_codec) < 0)
{
esyslog("could not open codec for AC3");
av_free(ac3_context);
ac3_context=NULL;
}
}
else
{
esyslog("could not allocate ac3 context");
}
}
else
{
esyslog("codec for AC3 not found");
ac3_context=NULL;
}
}
else
{
ac3_context=NULL;
}
AVCodec *video_codec=NULL;
CodecID video_codecid;
if (useH264)
{
video_codecid=CODEC_ID_H264;
}
else
{
video_codecid=CODEC_ID_MPEG2VIDEO_XVMC;
}
video_codec = avcodec_find_decoder(video_codecid);
if ((!video_codec) && (video_codecid==CODEC_ID_MPEG2VIDEO_XVMC))
{
// fallback to MPEG2VIDEO
video_codecid=CODEC_ID_MPEG2VIDEO;
video_codec=avcodec_find_decoder(video_codecid);
}
if (video_codec)
{
video_context = avcodec_alloc_context();
if (video_context)
{
video_context->thread_count=cpucount;
if (video_codec->capabilities & CODEC_CAP_TRUNCATED)
video_context->flags|=CODEC_FLAG_TRUNCATED; // we do not send complete frames
video_context->flags|=CODEC_FLAG_GRAY; // only decode grayscale
video_context->flags2|=CODEC_FLAG2_FAST; // really?
video_context->skip_idct=AVDISCARD_ALL;
if (video_codecid==CODEC_ID_H264)
{
video_context->flags2|=CODEC_FLAG2_CHUNKS; // needed for H264!
video_context->skip_loop_filter=AVDISCARD_ALL; // skip deblocking
av_log_set_level(AV_LOG_FATAL); // H264 decoder is very chatty
}
else
{
video_context->skip_frame=AVDISCARD_NONKEY; // just I-frames
}
video_context->codec_id = video_codecid;
video_context->codec_type = CODEC_TYPE_VIDEO;
int ret=avcodec_open(video_context, video_codec);
if ((ret < 0) && (video_codecid==CODEC_ID_MPEG2VIDEO_XVMC))
{
// fallback to MPEG2VIDEO
video_codecid=CODEC_ID_MPEG2VIDEO;
video_codec=avcodec_find_decoder(video_codecid);
if (video_codec)
{
video_context->codec_type=CODEC_TYPE_UNKNOWN;
video_context->codec_id=CODEC_ID_NONE;
video_context->codec_tag=0;
memset(video_context->codec_name,0,sizeof(video_context->codec_name));
ret=avcodec_open(video_context, video_codec);
}
else
{
ret=-1;
}
}
if (ret < 0)
{
switch (video_codecid)
{
case CODEC_ID_H264:
esyslog("could not open codec for H264");
break;
case CODEC_ID_MPEG2VIDEO_XVMC:
esyslog("could not open codec MPEG2 (XVMC)");
break;
case CODEC_ID_MPEG2VIDEO:
esyslog("could not open codec MPEG2");
break;
default:
esyslog("could not open video codec");
break;
}
av_free(video_context);
video_context=NULL;
}
else
{
isyslog("using codec %s",video_codec->long_name);
#if LIBAVCODEC_VERSION_INT >= ((52<<16)+(22<<8)+2)
if (video_context->hwaccel)
{
isyslog("using hwaccel %s",video_context->hwaccel->name);
}
#endif
video_frame = avcodec_alloc_frame();
if (!video_frame)
{
esyslog("could not allocate frame");
avcodec_close(video_context);
av_free(video_context);
video_context=NULL;
}
}
}
else
{
esyslog("could not allocate video context");
}
}
else
{
switch (video_codecid)
{
case CODEC_ID_H264:
esyslog("codec for H264 not found");
break;
case CODEC_ID_MPEG2VIDEO_XVMC:
esyslog("codec for MPEG2 (XVMC) not found");
break;
case CODEC_ID_MPEG2VIDEO:
esyslog("codec for MPEG2 not found");
break;
default:
esyslog("video codec not found");
break;
}
video_context=NULL;
}
if ((ac3_context) || (mp2_context))
{
audiobuf=(int16_t*) malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
}
else
{
audiobuf=NULL;
}
}
cMarkAdDecoder::~cMarkAdDecoder()
{
if (video_context)
{
avcodec_close(video_context);
av_free(video_context);
av_free(video_frame);
}
if (ac3_context)
{
avcodec_close(ac3_context);
av_free(ac3_context);
}
if (mp2_context)
{
avcodec_close(mp2_context);
av_free(mp2_context);
}
if (audiobuf) free(audiobuf);
}
bool cMarkAdDecoder::DecodeMP2(MarkAdContext *maContext, uchar *espkt, int eslen)
{
if (!mp2_context) return false;
maContext->Audio.Data.Valid=false;
AVPacket avpkt;
#if LIBAVCODEC_VERSION_INT >= ((52<<16)+(25<<8)+0)
av_init_packet(&avpkt);
#else
memset(&avpkt,0,sizeof(avpkt));
avpkt.pts = avpkt.dts = AV_NOPTS_VALUE;
avpkt.pos = -1;
#endif
avpkt.data=espkt;
avpkt.size=eslen;
audiobufsize=AVCODEC_MAX_AUDIO_FRAME_SIZE;
int ret=false;
int16_t Taudiobuf[AVCODEC_MAX_AUDIO_FRAME_SIZE];
while (avpkt.size>0)
{
#if LIBAVCODEC_VERSION_INT < ((52<<16)+(25<<8)+0)
int len=avcodec_decode_audio2(mp2_context,Taudiobuf,&audiobufsize,
avpkt.data,avpkt.size);
#else
int len=avcodec_decode_audio3(mp2_context,Taudiobuf,&audiobufsize,&avpkt);
#endif
if (len<0)
{
esyslog("error decoding mp2");
break;
}
if (audiobufsize>0)
{
memcpy(audiobuf,Taudiobuf,audiobufsize);
SetAudioInfos(maContext,mp2_context);
ret=true;
avpkt.size-=len;
avpkt.data+=len;
}
}
return ret;
}
bool cMarkAdDecoder::SetAudioInfos(MarkAdContext *maContext, AVCodecContext *Audio_Context)
{
if ((!maContext) || (!Audio_Context)) return false;
maContext->Audio.Info.SampleRate = Audio_Context->sample_rate;
maContext->Audio.Info.Channels = Audio_Context->channels;
maContext->Audio.Data.SampleBuf=audiobuf;
maContext->Audio.Data.SampleBufLen=audiobufsize;
maContext->Audio.Data.Valid=true;
return true;
}
bool cMarkAdDecoder::DecodeAC3(MarkAdContext *maContext, uchar *espkt, int eslen)
{
if (!ac3_context) return false;
maContext->Audio.Data.Valid=false;
AVPacket avpkt;
#if LIBAVCODEC_VERSION_INT >= ((52<<16)+(25<<8)+0)
av_init_packet(&avpkt);
#else
memset(&avpkt,0,sizeof(avpkt));
avpkt.pts = avpkt.dts = AV_NOPTS_VALUE;
avpkt.pos = -1;
#endif
avpkt.data=espkt;
avpkt.size=eslen;
int ret=false;
int16_t Taudiobuf[AVCODEC_MAX_AUDIO_FRAME_SIZE];
while (avpkt.size>0)
{
audiobufsize=AVCODEC_MAX_AUDIO_FRAME_SIZE;
#if LIBAVCODEC_VERSION_INT < ((52<<16)+(25<<8)+0)
int len=avcodec_decode_audio2(ac3_context,Taudiobuf,&audiobufsize,
avpkt.data,avpkt.size);
#else
int len=avcodec_decode_audio3(ac3_context,Taudiobuf,&audiobufsize,&avpkt);
#endif
if (len<0)
{
esyslog("error decoding ac3");
break;
}
if (audiobufsize>0)
{
memcpy(audiobuf,Taudiobuf,audiobufsize);
SetAudioInfos(maContext,ac3_context);
ret=true;
avpkt.size-=len;
avpkt.data+=len;
}
}
return ret;
}
void cMarkAdDecoder::PAR2DAR(AVRational a, AVRational *erg)
{
av_reduce(&erg->num,&erg->den,video_context->width*a.num,
video_context->height*a.den,1024*1024);
}
bool cMarkAdDecoder::SetVideoInfos(MarkAdContext *maContext,AVCodecContext *Video_Context, AVFrame *Video_Frame)
{
if ((!maContext) || (!Video_Context) || (!Video_Frame)) return false;
for (int i=0; i<4; i++)
{
if (Video_Frame->data[i])
{
maContext->Video.Data.Plane[i]=Video_Frame->data[i];
maContext->Video.Data.PlaneLinesize[i]=Video_Frame->linesize[i];
maContext->Video.Data.Valid=true;
}
}
maContext->Video.Info.Height=Video_Context->height;
maContext->Video.Info.Width=Video_Context->width;
AVRational dar;
PAR2DAR(Video_Context->sample_aspect_ratio,&dar);
maContext->Video.Info.AspectRatio.Num=dar.num;
maContext->Video.Info.AspectRatio.Den=dar.den;
return true;
}
bool cMarkAdDecoder::DecodeVideo(MarkAdContext *maContext,uchar *pkt, int plen)
{
if (!video_context) return false;
maContext->Video.Data.Valid=false;
if ((video_context->codec_id==CODEC_ID_H264) && (!video_context->skip_frame))
{
if (maContext->Video.Info.Pict_Type)
{
if (maContext->Video.Info.Interlaced)
{
video_context->skip_frame=AVDISCARD_BIDIR; // just P/I-frames
}
else
{
video_context->skip_frame=AVDISCARD_NONKEY; // just I-frames
}
}
}
AVPacket avpkt;
#if LIBAVCODEC_VERSION_INT >= ((52<<16)+(25<<8)+0)
av_init_packet(&avpkt);
#else
memset(&avpkt,0,sizeof(avpkt));
avpkt.pts = avpkt.dts = AV_NOPTS_VALUE;
avpkt.pos = -1;
#endif
avpkt.data=pkt;
avpkt.size=plen;
// decode video
int video_frame_ready=0;
int len,ret=false;
while (avpkt.size>0)
{
#if LIBAVCODEC_VERSION_INT < ((52<<16)+(25<<8)+0)
len=avcodec_decode_video(video_context,video_frame,&video_frame_ready,
avpkt.data,avpkt.size);
#else
len=avcodec_decode_video2(video_context,video_frame,&video_frame_ready,
&avpkt);
#endif
if (len<0)
{
esyslog("error decoding video");
break;
}
else
{
avpkt.size-=len;
avpkt.data+=len;
}
if (video_frame_ready)
{
if (last_qscale_table!=video_frame->qscale_table)
{
if (SetVideoInfos(maContext,video_context,video_frame)) ret=true;
last_qscale_table=video_frame->qscale_table;
}
}
}
return ret;
}
|