-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcodec.c
657 lines (572 loc) · 16.9 KB
/
codec.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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
///
/// @file codec.c @brief Codec functions
///
/// Copyright (c) 2009 - 2015 by Johns. All Rights Reserved.
/// Copyright (c) 2018 by zille. All Rights Reserved.
///
/// Contributor(s):
///
/// License: AGPLv3
///
/// This program is free software: you can redistribute it and/or modify
/// it under the terms of the GNU Affero General Public License as
/// published by the Free Software Foundation, either version 3 of the
/// License.
///
/// This program is distributed in the hope that it will be useful,
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
/// GNU Affero General Public License for more details.
///
/// $Id$
//////////////////////////////////////////////////////////////////////////////
///
/// @defgroup Codec The codec module.
///
/// This module contains all decoder and codec functions.
/// It is uses ffmpeg (http://ffmpeg.org) as backend.
///
/// compile with pass-through support (stable, AC-3, E-AC-3 only)
#define USE_PASSTHROUGH
#include <unistd.h>
#include <libintl.h>
#define _(str) gettext(str) ///< gettext shortcut
#define _N(str) str ///< gettext_noop shortcut
#include <pthread.h>
#include <libavcodec/avcodec.h>
#ifdef MAIN_H
#include MAIN_H
#endif
#include "iatomic.h"
#include "misc.h"
#include "video.h"
#include "audio.h"
#include "codec.h"
#include "softhddev.h"
//----------------------------------------------------------------------------
// Global
//----------------------------------------------------------------------------
static pthread_mutex_t CodecLockMutex;
//----------------------------------------------------------------------------
// Video
//----------------------------------------------------------------------------
///
/// Video decoder structure.
///
struct _video_decoder_
{
VideoRender *Render; ///< video hardware decoder
AVCodecContext *VideoCtx; ///< video codec context
AVFrame *Frame; ///< decoded video frame
};
//----------------------------------------------------------------------------
// Call-backs
//----------------------------------------------------------------------------
/**
** Callback to negotiate the PixelFormat.
**
** @param video_ctx codec context
** @param fmt is the list of formats which are supported by
** the codec, it is terminated by -1 as 0 is a
** valid format, the formats are ordered by quality.
*/
static enum AVPixelFormat Codec_get_format(AVCodecContext * video_ctx,
const enum AVPixelFormat *fmt)
{
VideoDecoder *decoder;
decoder = video_ctx->opaque;
return Video_get_format(decoder->Render, video_ctx, fmt);
}
AVCodecContext *Codec_get_VideoContext(VideoDecoder * decoder)
{
return decoder->VideoCtx;
}
//----------------------------------------------------------------------------
// Test
//----------------------------------------------------------------------------
/**
** Allocate a new video decoder context.
**
** @param hw_decoder video hardware decoder
**
** @returns private decoder pointer for video decoder.
*/
VideoDecoder *CodecVideoNewDecoder(VideoRender * render)
{
VideoDecoder *decoder;
if (!(decoder = calloc(1, sizeof(*decoder)))) {
Fatal(_("codec: can't allocate vodeo decoder\n"));
}
decoder->Render = render;
return decoder;
}
/**
** Deallocate a video decoder context.
**
** @param decoder private video decoder
*/
void CodecVideoDelDecoder(VideoDecoder * decoder)
{
free(decoder);
}
/**
** Open video decoder.
**
** @param decoder private video decoder
** @param codec_id video codec id
*/
void CodecVideoOpen(VideoDecoder * decoder, int codec_id, AVCodecParameters * Par,
AVRational * timebase)
{
AVCodec * codec;
enum AVHWDeviceType type = 0;
static AVBufferRef *hw_device_ctx = NULL;
int err;
if (VideoCodecMode(decoder->Render) == 1 ||
(VideoCodecMode(decoder->Render) == 3 && codec_id == AV_CODEC_ID_H264)) {
if (!(codec = avcodec_find_decoder_by_name(VideoGetDecoderName(
avcodec_get_name(codec_id)))))
fprintf(stderr, "CodecVideoOpen: The video codec %s is not present in libavcodec\n",
VideoGetDecoderName(avcodec_get_name(codec_id)));
} else {
if (!(codec = avcodec_find_decoder(codec_id)))
fprintf(stderr, "CodecVideoOpen: The video codec %s is not present in libavcodec\n",
avcodec_get_name(codec_id));
if (!(VideoCodecMode(decoder->Render) == 2 && codec_id == AV_CODEC_ID_MPEG2VIDEO)) {
for (int n = 0; ; n++) {
const AVCodecHWConfig *cfg = avcodec_get_hw_config(codec, n);
if (!cfg) {
#ifdef CODEC_DEBUG
fprintf(stderr, "CodecVideoOpen: no HW config found\n");
#endif
break;
}
if (cfg->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX &&
cfg->device_type == AV_HWDEVICE_TYPE_DRM) {
#ifdef CODEC_DEBUG
fprintf(stderr, "CodecVideoOpen: HW codec %s found\n",
av_hwdevice_get_type_name(cfg->device_type));
#endif
type = cfg->device_type;
break;
}
}
}
}
#ifdef CODEC_DEBUG
fprintf(stderr, "CodecVideoOpen: Codec %s found\n", codec->long_name);
#endif
decoder->VideoCtx = avcodec_alloc_context3(codec);
if (!decoder->VideoCtx) {
fprintf(stderr, "CodecVideoOpen: can't open video codec!\n");
}
decoder->VideoCtx->codec_id = codec_id;
decoder->VideoCtx->get_format = Codec_get_format;
decoder->VideoCtx->opaque = decoder;
if (strstr(codec->name, "_v4l2")) {
int width;
int height;
if (!Par) {
ParseResolutionH264(&width, &height);
#ifdef CODEC_DEBUG
fprintf(stderr, "CodecVideoOpen: Parsed width %d height %d\n", width, height);
#endif
decoder->VideoCtx->coded_width = width;
decoder->VideoCtx->coded_height = height;
} else {
decoder->VideoCtx->coded_width = Par->width;
decoder->VideoCtx->coded_height = Par->height;
}
}
// decoder->VideoCtx->flags |= AV_CODEC_FLAG_BITEXACT;
// decoder->VideoCtx->flags2 |= AV_CODEC_FLAG2_FAST;
// decoder->VideoCtx->flags |= AV_CODEC_FLAG_TRUNCATED;
// if (codec->capabilities & AV_CODEC_CAP_DR1)
// fprintf(stderr, "[CodecVideoOpen] AV_CODEC_CAP_DR1 => get_buffer()\n");
if (codec->capabilities & AV_CODEC_CAP_FRAME_THREADS ||
AV_CODEC_CAP_SLICE_THREADS) {
decoder->VideoCtx->thread_count = 4;
#ifdef CODEC_DEBUG
fprintf(stderr, "CodecVideoOpen: decoder use %d threads\n",
decoder->VideoCtx->thread_count);
#endif
}
if (codec->capabilities & AV_CODEC_CAP_SLICE_THREADS){
decoder->VideoCtx->thread_type = FF_THREAD_SLICE;
#ifdef CODEC_DEBUG
fprintf(stderr, "CodecVideoOpen: decoder use THREAD_SLICE threads\n");
#endif
}
if (type) {
if (av_hwdevice_ctx_create(&hw_device_ctx, type, NULL, NULL, 0) < 0)
fprintf(stderr, "CodecVideoOpen: Error init the HW decoder\n");
else
decoder->VideoCtx->hw_device_ctx = av_buffer_ref(hw_device_ctx);
}
if (Par) {
if ((avcodec_parameters_to_context(decoder->VideoCtx, Par)) < 0)
fprintf(stderr, "CodecVideoOpen: insert parameters to context failed!\n");
}
if (timebase) {
decoder->VideoCtx->pkt_timebase.num = timebase->num;
decoder->VideoCtx->pkt_timebase.den = timebase->den;
}
err = avcodec_open2(decoder->VideoCtx, decoder->VideoCtx->codec, NULL);
if (err < 0) {
fprintf(stderr, "CodecVideoOpen: Error opening the decoder: %s\n",
av_err2str(err));
Fatal(_("CodecVideoOpen: Error opening the decoder: %s\n"),
av_err2str(err));
}
}
/**
** Close video decoder.
**
** @param decoder private video decoder
*/
void CodecVideoClose(VideoDecoder * decoder)
{
#ifdef DEBUG
fprintf(stderr, "CodecVideoClose: VideoCtx %p\n", decoder->VideoCtx);
#endif
pthread_mutex_lock(&CodecLockMutex);
if (decoder->VideoCtx) {
avcodec_free_context(&decoder->VideoCtx);
}
pthread_mutex_unlock(&CodecLockMutex);
}
/**
** Decode a video packet.
**
** @param decoder video decoder data
** @param avpkt video packet
**
** @returns 1 if packet must send again.
*/
int CodecVideoSendPacket(VideoDecoder * decoder, const AVPacket * avpkt)
{
int ret = AVERROR_DECODER_NOT_FOUND;
#if 0
if (!decoder->VideoCtx->extradata_size) {
AVBSFContext *bsf_ctx;
const AVBitStreamFilter *f;
int extradata_size;
uint8_t *extradata;
f = av_bsf_get_by_name("extract_extradata");
if (!f)
fprintf(stderr, "extradata av_bsf_get_by_name failed!\n");
if (av_bsf_alloc(f, &bsf_ctx) < 0)
fprintf(stderr, "extradata av_bsf_alloc failed!\n");
bsf_ctx->par_in->codec_id = decoder->VideoCtx->codec_id;
if (av_bsf_init(bsf_ctx) < 0)
fprintf(stderr, "extradata av_bsf_init failed!\n");
if (av_bsf_send_packet(bsf_ctx, avpkt) < 0)
fprintf(stderr, "extradata av_bsf_send_packet failed!\n");
if (av_bsf_receive_packet(bsf_ctx, avpkt) < 0)
fprintf(stderr, "extradata av_bsf_send_packet failed!\n");
extradata = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA,
&extradata_size);
decoder->VideoCtx->extradata = av_mallocz(extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
memcpy(decoder->VideoCtx->extradata, extradata, extradata_size);
decoder->VideoCtx->extradata_size = extradata_size;
av_bsf_free(&bsf_ctx);
fprintf(stderr, "extradata %p %d\n", decoder->VideoCtx->extradata, decoder->VideoCtx->extradata_size);
}
#endif
if (!avpkt->size) {
return 0;
}
pthread_mutex_lock(&CodecLockMutex);
if (decoder->VideoCtx) {
ret = avcodec_send_packet(decoder->VideoCtx, avpkt);
}
pthread_mutex_unlock(&CodecLockMutex);
#ifdef DEBUG
if (ret < 0)
fprintf(stderr, "CodecVideoSendPacket: send_packet ret: %s\n",
av_err2str(ret));
#endif
if (ret == AVERROR(EAGAIN))
return 1;
return 0;
}
/**
** Get a decoded a video frame.
**
** @param decoder video decoder data
** @param no_deint set interlaced_frame to 0
**
** @returns 1 get no frame.
*/
int CodecVideoReceiveFrame(VideoDecoder * decoder, int no_deint)
{
int ret;
if (!(decoder->Frame = av_frame_alloc())) {
Fatal(_("CodecVideoReceiveFrame: can't allocate decoder frame\n"));
}
pthread_mutex_lock(&CodecLockMutex);
if (decoder->VideoCtx) {
ret = avcodec_receive_frame(decoder->VideoCtx, decoder->Frame);
} else {
av_frame_free(&decoder->Frame);
pthread_mutex_unlock(&CodecLockMutex);
return 1;
}
pthread_mutex_unlock(&CodecLockMutex);
if (decoder->Frame->flags == AV_FRAME_FLAG_CORRUPT)
fprintf(stderr, "CodecVideoReceiveFrame: AV_FRAME_FLAG_CORRUPT\n");
if (!ret) {
if (no_deint) {
decoder->Frame->interlaced_frame = 0;
#ifdef STILL_DEBUG
fprintf(stderr, "CodecVideoReceiveFrame: interlaced_frame = 0\n");
#endif
}
VideoRenderFrame(decoder->Render, decoder->VideoCtx, decoder->Frame);
} else {
av_frame_free(&decoder->Frame);
#ifdef DEBUG
fprintf(stderr, "CodecVideoReceiveFrame: receive_frame ret: %s\n",
av_err2str(ret));
#endif
}
if (ret == AVERROR(EAGAIN))
return 1;
return 0;
}
/**
** Flush the video decoder.
**
** @param decoder video decoder data
*/
void CodecVideoFlushBuffers(VideoDecoder * decoder)
{
#ifdef DEBUG
fprintf(stderr, "CodecVideoFlushBuffers: VideoCtx %p\n", decoder->VideoCtx);
#endif
pthread_mutex_lock(&CodecLockMutex);
if (decoder->VideoCtx) {
avcodec_flush_buffers(decoder->VideoCtx);
}
pthread_mutex_unlock(&CodecLockMutex);
}
//----------------------------------------------------------------------------
// Audio
//----------------------------------------------------------------------------
///
/// Audio decoder structure.
///
struct _audio_decoder_
{
AVCodecContext *AudioCtx; ///< audio codec context
AVFrame *Frame; ///< decoded audio frame buffer
int64_t last_pts; ///< last PTS
};
///
/// IEC Data type enumeration.
///
enum IEC61937
{
IEC61937_AC3 = 0x01, ///< AC-3 data
// FIXME: more data types
IEC61937_EAC3 = 0x15, ///< E-AC-3 data
};
#ifdef USE_PASSTHROUGH
///
/// Pass-through flags: CodecPCM, CodecAC3, CodecEAC3, ...
///
static char CodecPassthrough;
#else
static const int CodecPassthrough = 0;
#endif
/**
** Allocate a new audio decoder context.
**
** @returns private decoder pointer for audio decoder.
*/
AudioDecoder *CodecAudioNewDecoder(void)
{
AudioDecoder *audio_decoder;
if (!(audio_decoder = calloc(1, sizeof(*audio_decoder)))) {
Fatal(_("codec: can't allocate audio decoder\n"));
}
if (!(audio_decoder->Frame = av_frame_alloc())) {
Fatal(_("codec: can't allocate audio decoder frame buffer\n"));
}
audio_decoder->AudioCtx = NULL;
return audio_decoder;
}
/**
** Deallocate an audio decoder context.
**
** @param decoder private audio decoder
*/
void CodecAudioDelDecoder(AudioDecoder * decoder)
{
av_frame_free(&decoder->Frame); // callee does checks
free(decoder);
}
/**
** Open audio decoder.
**
** @param audio_decoder private audio decoder
** @param codec_id audio codec id
*/
void CodecAudioOpen(AudioDecoder * audio_decoder, int codec_id,
AVCodecParameters *Par, AVRational * timebase)
{
AVCodec *codec;
if (codec_id == AV_CODEC_ID_AC3) {
if (!(codec = avcodec_find_decoder_by_name("ac3_fixed"))) {
Fatal(_("codec: codec ac3_fixed ID %#06x not found\n"), codec_id);
}
} else if (codec_id == AV_CODEC_ID_AAC) {
if (!(codec = avcodec_find_decoder_by_name("aac_fixed"))) {
Fatal(_("codec: codec aac_fixed ID %#06x not found\n"), codec_id);
}
} else {
if (!(codec = avcodec_find_decoder(codec_id))) {
Fatal(_("codec: codec %s ID %#06x not found\n"),
avcodec_get_name(codec_id), codec_id);
// FIXME: errors aren't fatal
}
}
if (!(audio_decoder->AudioCtx = avcodec_alloc_context3(codec))) {
Fatal(_("codec: can't allocate audio codec context\n"));
}
audio_decoder->AudioCtx->pkt_timebase.num = timebase->num;
audio_decoder->AudioCtx->pkt_timebase.den = timebase->den;
if (Par) {
if ((avcodec_parameters_to_context(audio_decoder->AudioCtx, Par)) < 0)
fprintf(stderr, "CodecAudioOpen: insert parameters to context failed!\n");
}
// open codec
if (avcodec_open2(audio_decoder->AudioCtx, audio_decoder->AudioCtx->codec, NULL) < 0) {
Fatal(_("codec: can't open audio codec\n"));
}
#ifdef CODEC_DEBUG
Debug(3, "CodecAudioOpen: Codec %s found\n", audio_decoder->AudioCtx->codec->long_name);
fprintf(stderr,"CodecAudioOpen: Codec %s found\n", audio_decoder->AudioCtx->codec->long_name);
#endif
}
/**
** Close audio decoder.
**
** @param audio_decoder private audio decoder
*/
void CodecAudioClose(AudioDecoder * audio_decoder)
{
#ifdef DEBUG
fprintf(stderr, "CodecAudioClose\n");
#endif
if (audio_decoder->AudioCtx) {
avcodec_free_context(&audio_decoder->AudioCtx);
}
}
/**
** Set audio pass-through.
**
** @param mask enable mask (PCM, AC-3, E-AC-3)
*/
void CodecSetAudioPassthrough(int mask)
{
#ifdef USE_PASSTHROUGH
CodecPassthrough = mask & (CodecPCM | CodecAC3 | CodecEAC3);
#endif
(void)mask;
}
/**
** Decode an audio packet.
**
** @param audio_decoder audio decoder data
** @param avpkt audio packet
*/
void CodecAudioDecode(AudioDecoder * audio_decoder, const AVPacket * avpkt)
{
AVFrame *frame;
int ret_send, ret_rec;
// FIXME: don't need to decode pass-through codecs
frame = audio_decoder->Frame;
av_frame_unref(frame);
send:
ret_send = avcodec_send_packet(audio_decoder->AudioCtx, avpkt);
if (ret_send < 0)
fprintf(stderr, "CodecAudioDecode: avcodec_send_packet error: %s\n",
av_err2str(ret_send));
ret_rec = avcodec_receive_frame(audio_decoder->AudioCtx, frame);
if (ret_rec < 0) {
fprintf(stderr, "CodecAudioDecode: avcodec_receive_frame error: %s\n",
av_err2str(ret_rec));
} else {
// Control PTS is valid
if (audio_decoder->last_pts == (int64_t) AV_NOPTS_VALUE &&
frame->pts == (int64_t) AV_NOPTS_VALUE) {
fprintf(stderr, "CodecAudioDecode: NO VALID PTS\n");
}
// update audio clock
if (frame->pts != (int64_t) AV_NOPTS_VALUE) {
audio_decoder->last_pts = frame->pts;
} else if (audio_decoder->last_pts != (int64_t) AV_NOPTS_VALUE) {
frame->pts = audio_decoder->last_pts +
(int64_t)(frame->nb_samples /
av_q2d(audio_decoder->AudioCtx->pkt_timebase) /
frame->sample_rate);
audio_decoder->last_pts = frame->pts;
}
AudioFilter(frame, audio_decoder->AudioCtx);
}
if (ret_send == AVERROR(EAGAIN))
goto send;
}
/**
** Flush the audio decoder.
**
** @param decoder audio decoder data
*/
void CodecAudioFlushBuffers(AudioDecoder * decoder)
{
#ifdef DEBUG
fprintf(stderr, "CodecAudioFlushBuffers: \n");
#endif
if (decoder->AudioCtx) {
avcodec_flush_buffers(decoder->AudioCtx);
}
decoder->last_pts = AV_NOPTS_VALUE;
}
//----------------------------------------------------------------------------
// Codec
//----------------------------------------------------------------------------
/**
** Empty log callback
*/
static void CodecNoopCallback( __attribute__ ((unused))
void *ptr, __attribute__ ((unused))
int level, __attribute__ ((unused))
const char *fmt, __attribute__ ((unused)) va_list vl)
{
}
/**
** Codec init
*/
void CodecInit(void)
{
#ifndef DEBUG
// disable display ffmpeg error messages
av_log_set_callback(CodecNoopCallback);
#else
(void)CodecNoopCallback;
// av_log_set_level(AV_LOG_DEBUG);
// av_log_set_level(AV_LOG_ERROR );
#endif
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58,18,100)
avcodec_register_all(); // register all formats and codecs
#endif
pthread_mutex_init(&CodecLockMutex, NULL);
}
/**
** Codec exit.
*/
void CodecExit(void)
{
pthread_mutex_destroy(&CodecLockMutex);
}