summaryrefslogtreecommitdiff
path: root/decoder.cpp
blob: 15d72def2a6f0613b77d794987d9fcce1dbef26e (plain)
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
/*
 * decoder.cpp: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id$
 */

#include "decoder.h"

cMarkAdDecoder::cMarkAdDecoder(int RecvNumber, bool useH264, bool useMP2, bool hasAC3)
{
    recvnumber=RecvNumber;
    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);
    }

    isyslog("markad [%i]: using libavcodec.so.%s with %i threads",recvnumber,
            AV_STRINGIFY(LIBAVCODEC_VERSION),cpucount);

    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;
                if (avcodec_open(mp2_context, mp2_codec) < 0)
                {
                    esyslog("markad [%i]: could not open codec 0x%05x",recvnumber,mp2_codecid);
                    av_free(mp2_context);
                    mp2_context=NULL;
                }
            }
            else
            {
                esyslog("markad [%i]: could not allocate mp2 context",recvnumber);
            }
        }
        else
        {
            esyslog("markad [%i]: codec 0x%05x not found",recvnumber,mp2_codecid);
            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;
                if (avcodec_open(ac3_context, ac3_codec) < 0)
                {
                    esyslog("markad [%i]: could not open codec 0x%05x",recvnumber,ac3_codecid);
                    av_free(ac3_context);
                    ac3_context=NULL;
                }
            }
            else
            {
                esyslog("markad [%i]: could not allocate ac3 context",recvnumber);
            }
        }
        else
        {
            esyslog("markad [%i]: codec 0x%05x not found",recvnumber,ac3_codecid);
            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_EMU_EDGE; // now linesize should be the same as width

            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
                video_context->skip_frame=AVDISCARD_NONREF; // P/I-frames
            }
            else
            {
                video_context->skip_frame=AVDISCARD_NONKEY; // I-frames
            }

            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)
            {
                esyslog("markad [%i]: could not open codec 0x%05x",recvnumber,video_codecid);
                av_free(video_context);
                video_context=NULL;
            }
            else
            {
                video_frame = avcodec_alloc_frame();
                if (!video_frame)
                {
                    esyslog("markad [%i]: could not allocate frame",recvnumber);
                    avcodec_close(video_context);
                    av_free(video_context);
                    video_context=NULL;
                }
            }
        }
        else
        {
            esyslog("markad [%i]: could not allocate video context",recvnumber);
        }
    }
    else
    {
        esyslog("markad [%i]: codec 0x%05x not found",recvnumber,video_codecid);
        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);
    SetVideoInfos(NULL,NULL,NULL,NULL);
}

bool cMarkAdDecoder::DecodeMP2(MarkAdContext *maContext, uchar *espkt, int eslen)
{
    if (!mp2_context) return 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("markad [%i]: error decoding mp2",recvnumber);
            break;
        }
        if (audiobufsize>0)
        {
            memcpy(audiobuf,Taudiobuf,audiobufsize);
            SetAudioInfos(maContext,ac3_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.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;
    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("markad [%i]: error decoding ac3",recvnumber);
            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, AVRational *DAR)
{
    if ((!maContext) || (!Video_Context) || (!Video_Frame)) return false;
    maContext->Video.Data.Valid=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.Info.Height=Video_Context->height;
    maContext->Video.Info.Width=Video_Context->width;

    maContext->Video.Info.Pict_Type=Video_Context->coded_frame->pict_type;

    if (DAR)
    {
        maContext->Video.Info.AspectRatio.Num=DAR->num;
        maContext->Video.Info.AspectRatio.Den=DAR->den;
    }

    maContext->Video.Data.Valid=true;
    return true;
}

bool cMarkAdDecoder::DecodeVideo(MarkAdContext *maContext,uchar *pkt, int plen)
{
    if (!video_context) return 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=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("markad [%i]: error decoding video",recvnumber);
            break;
        }
        else
        {
            avpkt.size-=len;
            avpkt.data+=len;
        }
        if (video_frame_ready)
        {
            if (last_qscale_table!=video_frame->qscale_table)
            {
                AVRational dar;
                PAR2DAR(video_context->sample_aspect_ratio,&dar);
                if (SetVideoInfos(maContext,video_context,video_frame,&dar)) ret=true;
                last_qscale_table=video_frame->qscale_table;
            }
        }
    }
    return ret;
}