2018-08-19 11:45:46 +02:00
|
|
|
///
|
2019-10-26 18:42:19 +02:00
|
|
|
/// @file codec.c @brief Codec functions
|
2018-08-19 11:45:46 +02:00
|
|
|
///
|
2021-12-27 20:02:45 +01:00
|
|
|
/// Copyright (c) 2009 - 2015 by Johns. All Rights Reserved.
|
2018-08-19 11:45:46 +02:00
|
|
|
///
|
2019-10-26 18:42:19 +02:00
|
|
|
/// Contributor(s):
|
2018-08-19 11:45:46 +02:00
|
|
|
///
|
2019-10-26 18:42:19 +02:00
|
|
|
/// License: AGPLv3
|
2018-08-19 11:45:46 +02:00
|
|
|
///
|
2019-10-26 18:42:19 +02:00
|
|
|
/// 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.
|
2018-08-19 11:45:46 +02:00
|
|
|
///
|
2019-10-26 18:42:19 +02:00
|
|
|
/// 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.
|
2018-08-19 11:45:46 +02:00
|
|
|
///
|
2019-10-26 18:42:19 +02:00
|
|
|
/// $Id: d285eb28485bea02cd205fc8be47320dfe0376cf $
|
2018-08-19 11:45:46 +02:00
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
///
|
2019-10-26 18:42:19 +02:00
|
|
|
/// @defgroup Codec The codec module.
|
2018-08-19 11:45:46 +02:00
|
|
|
///
|
2019-10-26 18:42:19 +02:00
|
|
|
/// This module contains all decoder and codec functions.
|
|
|
|
/// It is uses ffmpeg (http://ffmpeg.org) as backend.
|
2018-08-19 11:45:46 +02:00
|
|
|
///
|
2019-10-26 18:42:19 +02:00
|
|
|
/// It may work with libav (http://libav.org), but the tests show
|
2021-12-27 20:02:45 +01:00
|
|
|
/// many bugs and incompatiblity in it. Don't use this shit.
|
2018-08-19 11:45:46 +02:00
|
|
|
///
|
|
|
|
|
2019-10-28 21:43:37 +01:00
|
|
|
/// compile with pass-through support (stable, AC-3, E-AC-3 only)
|
2018-08-19 11:45:46 +02:00
|
|
|
#define USE_PASSTHROUGH
|
2019-10-28 21:43:37 +01:00
|
|
|
/// compile audio drift correction support (very experimental)
|
2018-08-19 11:45:46 +02:00
|
|
|
#define USE_AUDIO_DRIFT_CORRECTION
|
2019-10-28 21:43:37 +01:00
|
|
|
/// compile AC-3 audio drift correction support (very experimental)
|
2018-08-19 11:45:46 +02:00
|
|
|
#define USE_AC3_DRIFT_CORRECTION
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#ifdef __FreeBSD__
|
|
|
|
#include <sys/endian.h>
|
|
|
|
#else
|
|
|
|
#include <endian.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <libintl.h>
|
2021-12-27 20:02:45 +01:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#define _(str) gettext(str) ///< gettext shortcut
|
|
|
|
#define _N(str) str ///< gettext_noop shortcut
|
2018-08-19 11:45:46 +02:00
|
|
|
|
|
|
|
#include <libavcodec/avcodec.h>
|
|
|
|
#include <libavutil/mem.h>
|
2021-12-27 20:02:45 +01:00
|
|
|
#include <libavutil/opt.h>
|
2019-04-05 07:20:52 +02:00
|
|
|
|
2018-08-19 11:45:46 +02:00
|
|
|
#include <libswresample/swresample.h>
|
|
|
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
2021-12-27 20:02:45 +01:00
|
|
|
// clang-format off
|
2018-08-19 11:45:46 +02:00
|
|
|
#include "iatomic.h"
|
|
|
|
#include "misc.h"
|
|
|
|
#include "video.h"
|
|
|
|
#include "audio.h"
|
|
|
|
#include "codec.h"
|
2021-12-27 20:02:45 +01:00
|
|
|
// clang-format on
|
2018-08-19 11:45:46 +02:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
2019-10-26 18:42:19 +02:00
|
|
|
// Global
|
2018-08-19 11:45:46 +02:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
2019-10-28 21:43:37 +01:00
|
|
|
///
|
|
|
|
/// ffmpeg lock mutex
|
|
|
|
///
|
|
|
|
/// new ffmpeg dislikes simultanous open/close
|
|
|
|
/// this breaks our code, until this is fixed use lock.
|
|
|
|
///
|
2018-08-19 11:45:46 +02:00
|
|
|
static pthread_mutex_t CodecLockMutex;
|
|
|
|
|
2019-10-28 21:43:37 +01:00
|
|
|
/// Flag prefer fast channel switch
|
2018-08-19 11:45:46 +02:00
|
|
|
char CodecUsePossibleDefectFrames;
|
2018-09-08 16:53:55 +02:00
|
|
|
AVBufferRef *hw_device_ctx;
|
2020-04-10 16:17:23 +02:00
|
|
|
|
2018-08-19 11:45:46 +02:00
|
|
|
//----------------------------------------------------------------------------
|
2019-10-26 18:42:19 +02:00
|
|
|
// Video
|
2018-08-19 11:45:46 +02:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
///
|
2019-10-26 18:42:19 +02:00
|
|
|
/// Video decoder typedef.
|
2018-08-19 11:45:46 +02:00
|
|
|
///
|
|
|
|
//typedef struct _video_decoder_ Decoder;
|
|
|
|
#endif
|
|
|
|
#if 0
|
|
|
|
///
|
2019-10-26 18:42:19 +02:00
|
|
|
/// Video decoder structure.
|
2018-08-19 11:45:46 +02:00
|
|
|
///
|
|
|
|
struct _video_decoder_
|
|
|
|
{
|
2021-12-27 20:02:45 +01:00
|
|
|
VideoHwDecoder *HwDecoder; ///< video hardware decoder
|
2018-08-19 11:45:46 +02:00
|
|
|
|
2021-12-27 20:02:45 +01:00
|
|
|
int GetFormatDone; ///< flag get format called!
|
|
|
|
AVCodec *VideoCodec; ///< video codec
|
|
|
|
AVCodecContext *VideoCtx; ///< video codec context
|
|
|
|
AVFrame *Frame; ///< decoded video frame
|
2018-08-19 11:45:46 +02:00
|
|
|
};
|
|
|
|
#endif
|
|
|
|
//----------------------------------------------------------------------------
|
2019-10-26 18:42:19 +02:00
|
|
|
// Call-backs
|
2018-08-19 11:45:46 +02:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2019-11-18 13:01:19 +01:00
|
|
|
** Callback to negotiate the PixelFormat.
|
2018-08-19 11:45:46 +02:00
|
|
|
**
|
2021-12-27 20:02:45 +01:00
|
|
|
** @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.
|
2018-08-19 11:45:46 +02:00
|
|
|
*/
|
2021-12-27 20:02:45 +01:00
|
|
|
static enum AVPixelFormat Codec_get_format(AVCodecContext *video_ctx, const enum AVPixelFormat *fmt) {
|
2018-08-19 11:45:46 +02:00
|
|
|
VideoDecoder *decoder;
|
2019-10-26 18:42:19 +02:00
|
|
|
enum AVPixelFormat fmt1;
|
|
|
|
|
2018-08-19 11:45:46 +02:00
|
|
|
decoder = video_ctx->opaque;
|
|
|
|
// bug in ffmpeg 1.1.1, called with zero width or height
|
|
|
|
if (!video_ctx->width || !video_ctx->height) {
|
2019-10-26 18:42:19 +02:00
|
|
|
Error("codec/video: ffmpeg/libav buggy: width or height zero\n");
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
2019-10-26 18:42:19 +02:00
|
|
|
|
2021-12-27 20:02:45 +01:00
|
|
|
// decoder->GetFormatDone = 1;
|
2018-08-19 11:45:46 +02:00
|
|
|
return Video_get_format(decoder->HwDecoder, video_ctx, fmt);
|
|
|
|
}
|
|
|
|
|
2019-10-28 21:43:37 +01:00
|
|
|
// static void Codec_free_buffer(void *opaque, uint8_t *data);
|
2018-08-19 11:45:46 +02:00
|
|
|
|
|
|
|
/**
|
2019-11-18 13:01:19 +01:00
|
|
|
** Video buffer management, get buffer for frame.
|
2018-08-19 11:45:46 +02:00
|
|
|
**
|
2019-11-18 13:01:19 +01:00
|
|
|
** Called at the beginning of each frame to get a buffer for it.
|
2018-08-19 11:45:46 +02:00
|
|
|
**
|
2021-12-27 20:02:45 +01:00
|
|
|
** @param video_ctx Codec context
|
|
|
|
** @param frame Get buffer for this frame
|
2018-08-19 11:45:46 +02:00
|
|
|
*/
|
2021-12-27 20:02:45 +01:00
|
|
|
static int Codec_get_buffer2(AVCodecContext *video_ctx, AVFrame *frame, int flags) {
|
2018-08-19 11:45:46 +02:00
|
|
|
VideoDecoder *decoder;
|
|
|
|
|
|
|
|
decoder = video_ctx->opaque;
|
|
|
|
|
2021-12-27 20:02:45 +01:00
|
|
|
if (!decoder->GetFormatDone) { // get_format missing
|
2019-10-26 18:42:19 +02:00
|
|
|
enum AVPixelFormat fmts[2];
|
2018-08-19 11:45:46 +02:00
|
|
|
|
2019-10-28 21:43:37 +01:00
|
|
|
// fprintf(stderr, "codec: buggy libav, use ffmpeg\n");
|
|
|
|
// Warning(_("codec: buggy libav, use ffmpeg\n"));
|
2019-10-26 18:42:19 +02:00
|
|
|
fmts[0] = video_ctx->pix_fmt;
|
|
|
|
fmts[1] = AV_PIX_FMT_NONE;
|
|
|
|
Codec_get_format(video_ctx, fmts);
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
2019-08-22 12:34:29 +02:00
|
|
|
#if 0
|
2019-10-26 18:42:19 +02:00
|
|
|
if (decoder->hwaccel_get_buffer && (AV_PIX_FMT_VDPAU == decoder->hwaccel_pix_fmt
|
2021-12-27 20:02:45 +01:00
|
|
|
|| AV_PIX_FMT_CUDA == decoder->hwaccel_pix_fmt || AV_PIX_FMT_VAAPI == decoder->hwaccel_pix_fmt)) {
|
|
|
|
// Debug(3,"hwaccel get_buffer\n");
|
|
|
|
return decoder->hwaccel_get_buffer(video_ctx, frame, flags);
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
2019-08-22 12:34:29 +02:00
|
|
|
#endif
|
2019-10-28 21:43:37 +01:00
|
|
|
// Debug(3, "codec: fallback to default get_buffer\n");
|
2018-08-19 11:45:46 +02:00
|
|
|
return avcodec_default_get_buffer2(video_ctx, frame, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
2019-10-26 18:42:19 +02:00
|
|
|
// Test
|
2018-08-19 11:45:46 +02:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2019-11-18 13:01:19 +01:00
|
|
|
** Allocate a new video decoder context.
|
2018-08-19 11:45:46 +02:00
|
|
|
**
|
2021-12-27 20:02:45 +01:00
|
|
|
** @param hw_decoder video hardware decoder
|
2018-08-19 11:45:46 +02:00
|
|
|
**
|
2019-11-18 13:01:19 +01:00
|
|
|
** @returns private decoder pointer for video decoder.
|
2018-08-19 11:45:46 +02:00
|
|
|
*/
|
2021-12-27 20:02:45 +01:00
|
|
|
VideoDecoder *CodecVideoNewDecoder(VideoHwDecoder *hw_decoder) {
|
2018-08-19 11:45:46 +02:00
|
|
|
VideoDecoder *decoder;
|
|
|
|
|
|
|
|
if (!(decoder = calloc(1, sizeof(*decoder)))) {
|
2019-10-26 18:42:19 +02:00
|
|
|
Fatal(_("codec: can't allocate vodeo decoder\n"));
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
decoder->HwDecoder = hw_decoder;
|
|
|
|
|
|
|
|
return decoder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-18 13:01:19 +01:00
|
|
|
** Deallocate a video decoder context.
|
2018-08-19 11:45:46 +02:00
|
|
|
**
|
2019-11-18 13:01:19 +01:00
|
|
|
** @param decoder private video decoder
|
2018-08-19 11:45:46 +02:00
|
|
|
*/
|
2021-12-27 20:02:45 +01:00
|
|
|
void CodecVideoDelDecoder(VideoDecoder *decoder) { free(decoder); }
|
2018-08-19 11:45:46 +02:00
|
|
|
|
|
|
|
/**
|
2019-11-18 13:01:19 +01:00
|
|
|
** Open video decoder.
|
2018-08-19 11:45:46 +02:00
|
|
|
**
|
2019-11-18 13:01:19 +01:00
|
|
|
** @param decoder private video decoder
|
|
|
|
** @param codec_id video codec id
|
2018-08-19 11:45:46 +02:00
|
|
|
*/
|
2021-12-27 20:02:45 +01:00
|
|
|
void CodecVideoOpen(VideoDecoder *decoder, int codec_id) {
|
2018-08-19 11:45:46 +02:00
|
|
|
AVCodec *video_codec;
|
|
|
|
const char *name;
|
2019-10-26 18:42:19 +02:00
|
|
|
int ret, deint = 2;
|
2018-08-19 11:45:46 +02:00
|
|
|
|
2018-08-20 17:25:39 +02:00
|
|
|
Debug(3, "***************codec: Video Open using video codec ID %#06x (%s)\n", codec_id,
|
2021-12-27 20:02:45 +01:00
|
|
|
avcodec_get_name(codec_id));
|
2018-08-19 11:45:46 +02:00
|
|
|
|
|
|
|
if (decoder->VideoCtx) {
|
2019-10-26 18:42:19 +02:00
|
|
|
Error(_("codec: missing close\n"));
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
2019-10-26 18:42:19 +02:00
|
|
|
|
2018-09-08 16:53:55 +02:00
|
|
|
name = "NULL";
|
2019-08-22 12:34:29 +02:00
|
|
|
#ifdef CUVID
|
2018-08-19 11:45:46 +02:00
|
|
|
if (!strcasecmp(VideoGetDriverName(), "cuvid")) {
|
2019-10-26 18:42:19 +02:00
|
|
|
switch (codec_id) {
|
|
|
|
case AV_CODEC_ID_MPEG2VIDEO:
|
|
|
|
name = "mpeg2_cuvid";
|
|
|
|
break;
|
|
|
|
case AV_CODEC_ID_H264:
|
|
|
|
name = "h264_cuvid";
|
|
|
|
break;
|
|
|
|
case AV_CODEC_ID_HEVC:
|
|
|
|
name = "hevc_cuvid";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-04-10 16:17:23 +02:00
|
|
|
#endif
|
2018-08-19 11:45:46 +02:00
|
|
|
if (name && (video_codec = avcodec_find_decoder_by_name(name))) {
|
2019-10-26 18:42:19 +02:00
|
|
|
Debug(3, "codec: decoder found\n");
|
|
|
|
} else if ((video_codec = avcodec_find_decoder(codec_id)) == NULL) {
|
|
|
|
Debug(3, "Decoder %s not supported %p\n", name, video_codec);
|
|
|
|
Fatal(_(" No decoder found"));
|
|
|
|
}
|
2018-08-19 11:45:46 +02:00
|
|
|
|
2018-10-04 16:49:06 +02:00
|
|
|
decoder->VideoCodec = video_codec;
|
2019-10-26 18:42:19 +02:00
|
|
|
|
|
|
|
Debug(3, "codec: video '%s'\n", decoder->VideoCodec->long_name);
|
|
|
|
|
2018-08-19 11:45:46 +02:00
|
|
|
if (!(decoder->VideoCtx = avcodec_alloc_context3(video_codec))) {
|
2019-10-26 18:42:19 +02:00
|
|
|
Fatal(_("codec: can't allocate video codec context\n"));
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
2020-04-10 16:17:23 +02:00
|
|
|
|
2020-01-15 18:01:27 +01:00
|
|
|
if (!HwDeviceContext) {
|
|
|
|
Fatal("codec: no hw device context to be used");
|
2018-09-28 17:02:56 +02:00
|
|
|
}
|
|
|
|
decoder->VideoCtx->hw_device_ctx = av_buffer_ref(HwDeviceContext);
|
|
|
|
|
2018-08-19 11:45:46 +02:00
|
|
|
// FIXME: for software decoder use all cpus, otherwise 1
|
|
|
|
decoder->VideoCtx->thread_count = 1;
|
2019-10-26 18:42:19 +02:00
|
|
|
|
|
|
|
decoder->VideoCtx->pkt_timebase.num = 1;
|
|
|
|
decoder->VideoCtx->pkt_timebase.den = 90000;
|
|
|
|
decoder->VideoCtx->framerate.num = 50;
|
|
|
|
decoder->VideoCtx->framerate.den = 1;
|
|
|
|
|
2018-08-19 11:45:46 +02:00
|
|
|
pthread_mutex_lock(&CodecLockMutex);
|
|
|
|
// open codec
|
2019-04-05 07:20:52 +02:00
|
|
|
#ifdef YADIF
|
2019-10-26 18:42:19 +02:00
|
|
|
deint = 2;
|
2019-04-05 07:20:52 +02:00
|
|
|
#endif
|
2023-02-13 10:11:12 +01:00
|
|
|
|
2022-05-24 00:09:55 +02:00
|
|
|
#if defined VAAPI
|
2023-02-13 10:11:12 +01:00
|
|
|
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59,8,100)
|
2020-01-31 12:28:18 +01:00
|
|
|
// decoder->VideoCtx->extra_hw_frames = 8; // VIDEO_SURFACES_MAX +1
|
2019-08-22 12:34:29 +02:00
|
|
|
if (video_codec->capabilities & (AV_CODEC_CAP_AUTO_THREADS)) {
|
2019-10-26 18:42:19 +02:00
|
|
|
Debug(3, "codec: auto threads enabled");
|
2021-12-27 20:02:45 +01:00
|
|
|
// decoder->VideoCtx->thread_count = 0;
|
2020-04-10 16:17:23 +02:00
|
|
|
}
|
2018-08-19 11:45:46 +02:00
|
|
|
|
2019-08-22 12:34:29 +02:00
|
|
|
if (video_codec->capabilities & AV_CODEC_CAP_TRUNCATED) {
|
2019-10-26 18:42:19 +02:00
|
|
|
Debug(3, "codec: supports truncated packets");
|
2021-12-27 20:02:45 +01:00
|
|
|
// decoder->VideoCtx->flags |= CODEC_FLAG_TRUNCATED;
|
2019-08-22 12:34:29 +02:00
|
|
|
}
|
2023-02-13 10:11:12 +01:00
|
|
|
#endif
|
2019-08-22 12:34:29 +02:00
|
|
|
// FIXME: own memory management for video frames.
|
|
|
|
if (video_codec->capabilities & AV_CODEC_CAP_DR1) {
|
2019-10-26 18:42:19 +02:00
|
|
|
Debug(3, "codec: can use own buffer management");
|
2019-08-22 12:34:29 +02:00
|
|
|
}
|
|
|
|
if (video_codec->capabilities & AV_CODEC_CAP_FRAME_THREADS) {
|
2019-10-26 18:42:19 +02:00
|
|
|
Debug(3, "codec: supports frame threads");
|
2021-12-27 20:02:45 +01:00
|
|
|
// decoder->VideoCtx->thread_count = 0;
|
2019-10-26 18:42:19 +02:00
|
|
|
// decoder->VideoCtx->thread_type |= FF_THREAD_FRAME;
|
2019-08-22 12:34:29 +02:00
|
|
|
}
|
|
|
|
if (video_codec->capabilities & AV_CODEC_CAP_SLICE_THREADS) {
|
2019-10-26 18:42:19 +02:00
|
|
|
Debug(3, "codec: supports slice threads");
|
2021-12-27 20:02:45 +01:00
|
|
|
// decoder->VideoCtx->thread_count = 0;
|
2019-10-26 18:42:19 +02:00
|
|
|
// decoder->VideoCtx->thread_type |= FF_THREAD_SLICE;
|
2019-08-22 12:34:29 +02:00
|
|
|
}
|
2021-12-27 20:02:45 +01:00
|
|
|
// if (av_opt_set_int(decoder->VideoCtx, "refcounted_frames", 1, 0) < 0)
|
|
|
|
// Fatal(_("VAAPI Refcounts invalid\n"));
|
2023-02-13 12:01:15 +01:00
|
|
|
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59,8,100)
|
2019-10-26 18:42:19 +02:00
|
|
|
decoder->VideoCtx->thread_safe_callbacks = 0;
|
2023-02-13 12:01:15 +01:00
|
|
|
#endif
|
2020-04-10 16:17:23 +02:00
|
|
|
|
2019-12-23 11:14:38 +01:00
|
|
|
#endif
|
2020-04-10 16:17:23 +02:00
|
|
|
|
2019-08-22 12:34:29 +02:00
|
|
|
#ifdef CUVID
|
2021-12-27 20:02:45 +01:00
|
|
|
if (strcmp(decoder->VideoCodec->long_name,
|
|
|
|
"Nvidia CUVID MPEG2VIDEO decoder") == 0) { // deinterlace for mpeg2 is somehow broken
|
2023-09-18 11:52:03 +02:00
|
|
|
if (av_opt_set_int(decoder->VideoCtx->priv_data, "deint", 1, 0) < 0) { // adaptive
|
2019-10-26 18:42:19 +02:00
|
|
|
pthread_mutex_unlock(&CodecLockMutex);
|
|
|
|
Fatal(_("codec: can't set option deint to video codec!\n"));
|
|
|
|
}
|
2023-09-18 11:52:03 +02:00
|
|
|
#if 0
|
|
|
|
if (av_opt_set_int(decoder->VideoCtx->priv_data, "surfaces", 13, 0) < 0) {
|
2019-10-26 18:42:19 +02:00
|
|
|
pthread_mutex_unlock(&CodecLockMutex);
|
|
|
|
Fatal(_("codec: can't set option surfces to video codec!\n"));
|
|
|
|
}
|
2018-10-21 17:24:12 +02:00
|
|
|
#endif
|
2019-10-26 18:42:19 +02:00
|
|
|
if (av_opt_set(decoder->VideoCtx->priv_data, "drop_second_field", "false", 0) < 0) {
|
|
|
|
pthread_mutex_unlock(&CodecLockMutex);
|
|
|
|
Fatal(_("codec: can't set option drop 2.field to video codec!\n"));
|
|
|
|
}
|
|
|
|
} else if (strstr(decoder->VideoCodec->long_name, "Nvidia CUVID") != NULL) {
|
2021-12-27 20:02:45 +01:00
|
|
|
if (av_opt_set_int(decoder->VideoCtx->priv_data, "deint", deint, 0) < 0) { // adaptive
|
2019-10-26 18:42:19 +02:00
|
|
|
pthread_mutex_unlock(&CodecLockMutex);
|
|
|
|
Fatal(_("codec: can't set option deint to video codec!\n"));
|
|
|
|
}
|
2019-04-05 07:20:52 +02:00
|
|
|
#if 1
|
2019-10-26 18:42:19 +02:00
|
|
|
if (av_opt_set_int(decoder->VideoCtx->priv_data, "surfaces", 13, 0) < 0) {
|
|
|
|
pthread_mutex_unlock(&CodecLockMutex);
|
|
|
|
Fatal(_("codec: can't set option surfces to video codec!\n"));
|
|
|
|
}
|
2019-04-05 07:20:52 +02:00
|
|
|
#endif
|
2019-10-26 18:42:19 +02:00
|
|
|
if (av_opt_set(decoder->VideoCtx->priv_data, "drop_second_field", "false", 0) < 0) {
|
|
|
|
pthread_mutex_unlock(&CodecLockMutex);
|
|
|
|
Fatal(_("codec: can't set option drop 2.field to video codec!\n"));
|
|
|
|
}
|
|
|
|
}
|
2019-08-22 12:34:29 +02:00
|
|
|
#endif
|
2018-08-19 11:45:46 +02:00
|
|
|
|
|
|
|
if ((ret = avcodec_open2(decoder->VideoCtx, video_codec, NULL)) < 0) {
|
2019-10-26 18:42:19 +02:00
|
|
|
pthread_mutex_unlock(&CodecLockMutex);
|
|
|
|
Fatal(_("codec: can't open video codec!\n"));
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
2019-10-26 18:42:19 +02:00
|
|
|
Debug(3, " Codec open %d\n", ret);
|
2018-08-19 11:45:46 +02:00
|
|
|
|
|
|
|
pthread_mutex_unlock(&CodecLockMutex);
|
|
|
|
|
2021-12-27 20:02:45 +01:00
|
|
|
decoder->VideoCtx->opaque = decoder; // our structure
|
2019-10-26 18:42:19 +02:00
|
|
|
|
2021-12-27 20:02:45 +01:00
|
|
|
// decoder->VideoCtx->debug = FF_DEBUG_STARTCODE;
|
|
|
|
// decoder->VideoCtx->err_recognition |= AV_EF_EXPLODE;
|
2018-08-19 11:45:46 +02:00
|
|
|
|
2021-12-27 20:02:45 +01:00
|
|
|
// av_log_set_level(AV_LOG_DEBUG);
|
2019-10-26 18:42:19 +02:00
|
|
|
av_log_set_level(0);
|
2018-08-19 11:45:46 +02:00
|
|
|
|
2019-10-26 18:42:19 +02:00
|
|
|
decoder->VideoCtx->get_format = Codec_get_format;
|
|
|
|
decoder->VideoCtx->get_buffer2 = Codec_get_buffer2;
|
2019-10-28 21:43:37 +01:00
|
|
|
// decoder->VideoCtx->active_thread_type = 0;
|
2019-10-26 18:42:19 +02:00
|
|
|
decoder->VideoCtx->draw_horiz_band = NULL;
|
|
|
|
decoder->VideoCtx->hwaccel_context = VideoGetHwAccelContext(decoder->HwDecoder);
|
2018-08-19 11:45:46 +02:00
|
|
|
|
|
|
|
//
|
2021-12-27 20:02:45 +01:00
|
|
|
// Prepare frame buffer for decoder
|
2018-08-19 11:45:46 +02:00
|
|
|
//
|
2019-08-22 12:34:29 +02:00
|
|
|
#if 0
|
2018-08-19 11:45:46 +02:00
|
|
|
if (!(decoder->Frame = av_frame_alloc())) {
|
2021-12-27 20:02:45 +01:00
|
|
|
Fatal(_("codec: can't allocate video decoder frame buffer\n"));
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
2019-08-22 12:34:29 +02:00
|
|
|
#endif
|
2019-10-26 18:42:19 +02:00
|
|
|
|
2018-08-19 11:45:46 +02:00
|
|
|
// reset buggy ffmpeg/libav flag
|
|
|
|
decoder->GetFormatDone = 0;
|
2022-05-24 00:09:55 +02:00
|
|
|
#if defined(YADIF)
|
2019-10-26 18:42:19 +02:00
|
|
|
decoder->filter = 0;
|
2019-04-05 07:20:52 +02:00
|
|
|
#endif
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-18 13:01:19 +01:00
|
|
|
** Close video decoder.
|
2018-08-19 11:45:46 +02:00
|
|
|
**
|
2019-11-18 13:01:19 +01:00
|
|
|
** @param video_decoder private video decoder
|
2018-08-19 11:45:46 +02:00
|
|
|
*/
|
2021-12-27 20:02:45 +01:00
|
|
|
void CodecVideoClose(VideoDecoder *video_decoder) {
|
2019-10-26 18:42:19 +02:00
|
|
|
AVFrame *frame;
|
|
|
|
|
2018-08-19 11:45:46 +02:00
|
|
|
// FIXME: play buffered data
|
2019-10-28 21:43:37 +01:00
|
|
|
// av_frame_free(&video_decoder->Frame); // callee does checks
|
2018-08-19 11:45:46 +02:00
|
|
|
|
2019-10-26 18:42:19 +02:00
|
|
|
Debug(3, "CodecVideoClose\n");
|
2018-08-19 11:45:46 +02:00
|
|
|
if (video_decoder->VideoCtx) {
|
2019-10-26 18:42:19 +02:00
|
|
|
pthread_mutex_lock(&CodecLockMutex);
|
|
|
|
#if 1
|
|
|
|
frame = av_frame_alloc();
|
|
|
|
avcodec_send_packet(video_decoder->VideoCtx, NULL);
|
2021-12-27 20:02:45 +01:00
|
|
|
while (avcodec_receive_frame(video_decoder->VideoCtx, frame) >= 0)
|
|
|
|
;
|
2019-10-26 18:42:19 +02:00
|
|
|
av_frame_free(&frame);
|
2019-10-20 14:48:28 +02:00
|
|
|
#endif
|
2019-10-26 18:42:19 +02:00
|
|
|
avcodec_close(video_decoder->VideoCtx);
|
|
|
|
av_freep(&video_decoder->VideoCtx);
|
|
|
|
pthread_mutex_unlock(&CodecLockMutex);
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
|
|
|
/**
|
2019-11-18 13:01:19 +01:00
|
|
|
** Display pts...
|
2018-08-19 11:45:46 +02:00
|
|
|
**
|
2019-11-18 13:01:19 +01:00
|
|
|
** ffmpeg-0.9 pts always AV_NOPTS_VALUE
|
|
|
|
** ffmpeg-0.9 pkt_pts nice monotonic (only with HD)
|
|
|
|
** ffmpeg-0.9 pkt_dts wild jumping -160 - 340 ms
|
2018-08-19 11:45:46 +02:00
|
|
|
**
|
2019-11-18 13:01:19 +01:00
|
|
|
** libav 0.8_pre20111116 pts always AV_NOPTS_VALUE
|
|
|
|
** libav 0.8_pre20111116 pkt_pts always 0 (could be fixed?)
|
|
|
|
** libav 0.8_pre20111116 pkt_dts wild jumping -160 - 340 ms
|
2018-08-19 11:45:46 +02:00
|
|
|
*/
|
|
|
|
void DisplayPts(AVCodecContext * video_ctx, AVFrame * frame)
|
|
|
|
{
|
|
|
|
int ms_delay;
|
|
|
|
int64_t pts;
|
|
|
|
static int64_t last_pts;
|
|
|
|
|
|
|
|
pts = frame->pkt_pts;
|
|
|
|
if (pts == (int64_t) AV_NOPTS_VALUE) {
|
2021-12-27 20:02:45 +01:00
|
|
|
printf("*");
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
ms_delay = (1000 * video_ctx->time_base.num) / video_ctx->time_base.den;
|
|
|
|
ms_delay += frame->repeat_pict * ms_delay / 2;
|
2021-12-27 20:02:45 +01:00
|
|
|
printf("codec: PTS %s%s %" PRId64 " %d %d/%d %d/%d %dms\n", frame->repeat_pict ? "r" : " ",
|
|
|
|
frame->interlaced_frame ? "I" : " ", pts, (int)(pts - last_pts) / 90, video_ctx->time_base.num,
|
|
|
|
video_ctx->time_base.den, video_ctx->framerate.num, video_ctx->framerate.den, ms_delay);
|
2018-08-19 11:45:46 +02:00
|
|
|
|
|
|
|
if (pts != (int64_t) AV_NOPTS_VALUE) {
|
2021-12-27 20:02:45 +01:00
|
|
|
last_pts = pts;
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
2019-11-18 13:01:19 +01:00
|
|
|
** Decode a video packet.
|
2018-08-19 11:45:46 +02:00
|
|
|
**
|
2019-11-18 13:01:19 +01:00
|
|
|
** @param decoder video decoder data
|
|
|
|
** @param avpkt video packet
|
2018-08-19 11:45:46 +02:00
|
|
|
*/
|
2018-10-08 17:02:12 +02:00
|
|
|
extern int CuvidTestSurfaces();
|
2019-10-26 18:42:19 +02:00
|
|
|
|
2021-12-27 20:02:45 +01:00
|
|
|
#if defined YADIF || defined(VAAPI)
|
|
|
|
extern int init_filters(AVCodecContext *dec_ctx, void *decoder, AVFrame *frame);
|
|
|
|
extern int push_filters(AVCodecContext *dec_ctx, void *decoder, AVFrame *frame);
|
2019-04-05 07:20:52 +02:00
|
|
|
#endif
|
2020-01-15 18:01:27 +01:00
|
|
|
|
2019-08-22 12:34:29 +02:00
|
|
|
#ifdef VAAPI
|
2021-12-27 20:02:45 +01:00
|
|
|
void CodecVideoDecode(VideoDecoder *decoder, const AVPacket *avpkt) {
|
2019-08-22 12:34:29 +02:00
|
|
|
AVCodecContext *video_ctx = decoder->VideoCtx;
|
|
|
|
|
2019-12-12 11:31:40 +01:00
|
|
|
if (video_ctx->codec_type == AVMEDIA_TYPE_VIDEO && CuvidTestSurfaces()) {
|
2019-10-26 18:42:19 +02:00
|
|
|
int ret;
|
|
|
|
AVPacket pkt[1];
|
|
|
|
AVFrame *frame;
|
2020-04-10 16:17:23 +02:00
|
|
|
|
2021-12-27 20:02:45 +01:00
|
|
|
*pkt = *avpkt; // use copy
|
2019-10-26 18:42:19 +02:00
|
|
|
ret = avcodec_send_packet(video_ctx, pkt);
|
|
|
|
if (ret < 0) {
|
|
|
|
return;
|
2020-04-10 16:17:23 +02:00
|
|
|
}
|
|
|
|
|
2020-01-15 18:01:27 +01:00
|
|
|
if (!CuvidTestSurfaces())
|
|
|
|
usleep(1000);
|
2020-04-10 16:17:23 +02:00
|
|
|
|
2020-01-15 18:01:27 +01:00
|
|
|
ret = 0;
|
2020-04-10 16:17:23 +02:00
|
|
|
while (ret >= 0 && CuvidTestSurfaces()) {
|
2020-01-15 18:01:27 +01:00
|
|
|
frame = av_frame_alloc();
|
|
|
|
ret = avcodec_receive_frame(video_ctx, frame);
|
|
|
|
|
|
|
|
if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
|
|
|
|
Debug(4, "codec: receiving video frame failed");
|
|
|
|
av_frame_free(&frame);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (ret >= 0) {
|
|
|
|
if (decoder->filter) {
|
|
|
|
if (decoder->filter == 1) {
|
|
|
|
if (init_filters(video_ctx, decoder->HwDecoder, frame) < 0) {
|
|
|
|
Debug(3, "video: Init of VAAPI deint Filter failed\n");
|
|
|
|
decoder->filter = 0;
|
|
|
|
} else {
|
|
|
|
Debug(3, "Init VAAPI deint ok\n");
|
|
|
|
decoder->filter = 2;
|
|
|
|
}
|
|
|
|
}
|
2021-12-27 20:02:45 +01:00
|
|
|
if (frame->interlaced_frame && decoder->filter == 2 &&
|
|
|
|
(frame->height != 720)) { // broken ZDF sends Interlaced flag
|
2020-01-15 18:01:27 +01:00
|
|
|
push_filters(video_ctx, decoder->HwDecoder, frame);
|
|
|
|
continue;
|
2019-10-26 18:42:19 +02:00
|
|
|
}
|
|
|
|
}
|
2020-01-15 18:01:27 +01:00
|
|
|
VideoRenderFrame(decoder->HwDecoder, video_ctx, frame);
|
|
|
|
} else {
|
|
|
|
av_frame_free(&frame);
|
|
|
|
return;
|
2019-10-26 18:42:19 +02:00
|
|
|
}
|
2020-04-10 16:17:23 +02:00
|
|
|
}
|
2019-08-22 12:34:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2020-01-15 18:01:27 +01:00
|
|
|
|
2019-10-26 18:42:19 +02:00
|
|
|
#ifdef CUVID
|
2019-08-22 12:34:29 +02:00
|
|
|
|
2021-12-27 20:02:45 +01:00
|
|
|
void CodecVideoDecode(VideoDecoder *decoder, const AVPacket *avpkt) {
|
2018-08-19 11:45:46 +02:00
|
|
|
AVCodecContext *video_ctx;
|
2019-10-26 18:42:19 +02:00
|
|
|
AVFrame *frame;
|
|
|
|
int ret, ret1;
|
2018-08-19 11:45:46 +02:00
|
|
|
int got_frame;
|
|
|
|
int consumed = 0;
|
2019-10-26 18:42:19 +02:00
|
|
|
static uint64_t first_time = 0;
|
2019-01-04 10:08:47 +01:00
|
|
|
const AVPacket *pkt;
|
2019-10-26 18:42:19 +02:00
|
|
|
|
2021-12-27 20:02:45 +01:00
|
|
|
next_part:
|
2018-08-19 11:45:46 +02:00
|
|
|
video_ctx = decoder->VideoCtx;
|
2019-08-22 12:34:29 +02:00
|
|
|
|
2021-12-27 20:02:45 +01:00
|
|
|
pkt = avpkt; // use copy
|
2018-08-19 11:45:46 +02:00
|
|
|
got_frame = 0;
|
2019-10-26 18:42:19 +02:00
|
|
|
|
2019-10-28 21:43:37 +01:00
|
|
|
// printf("decode packet %d\n",(GetusTicks()-first_time)/1000000);
|
2018-08-19 11:45:46 +02:00
|
|
|
ret1 = avcodec_send_packet(video_ctx, pkt);
|
2019-08-22 12:34:29 +02:00
|
|
|
|
2019-10-28 21:43:37 +01:00
|
|
|
// first_time = GetusTicks();
|
2019-10-26 18:42:19 +02:00
|
|
|
|
|
|
|
if (ret1 >= 0) {
|
|
|
|
consumed = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!CuvidTestSurfaces())
|
|
|
|
usleep(1000);
|
2019-08-22 12:34:29 +02:00
|
|
|
|
2021-12-27 20:02:45 +01:00
|
|
|
// printf("send packet to decode %s %04x\n",consumed?"ok":"Full",ret1);
|
2019-10-26 18:42:19 +02:00
|
|
|
|
|
|
|
if ((ret1 == AVERROR(EAGAIN) || ret1 == AVERROR_EOF || ret1 >= 0) && CuvidTestSurfaces()) {
|
|
|
|
ret = 0;
|
|
|
|
while ((ret >= 0) && CuvidTestSurfaces()) { // get frames until empty snd Surfaces avail.
|
|
|
|
frame = av_frame_alloc();
|
2021-12-27 20:02:45 +01:00
|
|
|
ret = avcodec_receive_frame(video_ctx, frame); // get new frame
|
|
|
|
if (ret >= 0) { // one is avail.
|
2019-10-26 18:42:19 +02:00
|
|
|
got_frame = 1;
|
|
|
|
} else {
|
|
|
|
got_frame = 0;
|
|
|
|
}
|
2021-12-27 20:02:45 +01:00
|
|
|
// printf("got %s packet from
|
|
|
|
// decoder\n",got_frame?"1":"no");
|
|
|
|
if (got_frame) { // frame completed
|
|
|
|
// printf("video frame pts %#012" PRIx64 "
|
|
|
|
//%dms\n",frame->pts,(int)(apts - frame->pts) / 90);
|
2019-10-26 18:42:19 +02:00
|
|
|
#ifdef YADIF
|
|
|
|
if (decoder->filter) {
|
|
|
|
if (decoder->filter == 1) {
|
|
|
|
if (init_filters(video_ctx, decoder->HwDecoder, frame) < 0) {
|
2023-09-18 11:52:03 +02:00
|
|
|
Debug(3,"video: Init of YADIF Filter failed\n");
|
2019-10-26 18:42:19 +02:00
|
|
|
decoder->filter = 0;
|
|
|
|
} else {
|
|
|
|
Debug(3, "Init YADIF ok\n");
|
|
|
|
decoder->filter = 2;
|
|
|
|
}
|
|
|
|
}
|
2021-12-27 20:02:45 +01:00
|
|
|
if (frame->interlaced_frame && decoder->filter == 2 &&
|
|
|
|
(frame->height != 720)) { // broken ZDF sends Interlaced flag
|
2019-10-26 18:42:19 +02:00
|
|
|
ret = push_filters(video_ctx, decoder->HwDecoder, frame);
|
2019-10-28 21:43:37 +01:00
|
|
|
// av_frame_unref(frame);
|
2019-10-26 18:42:19 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2019-10-28 21:43:37 +01:00
|
|
|
// DisplayPts(video_ctx, frame);
|
2019-10-26 18:42:19 +02:00
|
|
|
VideoRenderFrame(decoder->HwDecoder, video_ctx, frame);
|
2019-10-28 21:43:37 +01:00
|
|
|
// av_frame_unref(frame);
|
2019-10-26 18:42:19 +02:00
|
|
|
} else {
|
|
|
|
av_frame_free(&frame);
|
2019-10-28 21:43:37 +01:00
|
|
|
// printf("codec: got no frame %d send %d\n",ret,ret1);
|
2019-10-26 18:42:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!CuvidTestSurfaces()) {
|
|
|
|
usleep(1000);
|
|
|
|
}
|
|
|
|
} else {
|
2019-10-28 21:43:37 +01:00
|
|
|
// consumed = 1;
|
2019-10-26 18:42:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!consumed) {
|
2021-12-27 20:02:45 +01:00
|
|
|
goto next_part; // try again to stuff decoder
|
2019-10-26 18:42:19 +02:00
|
|
|
}
|
2019-08-22 12:34:29 +02:00
|
|
|
}
|
|
|
|
#endif
|
2018-08-19 11:45:46 +02:00
|
|
|
|
|
|
|
/**
|
2019-11-18 13:01:19 +01:00
|
|
|
** Flush the video decoder.
|
2018-08-19 11:45:46 +02:00
|
|
|
**
|
2019-11-18 13:01:19 +01:00
|
|
|
** @param decoder video decoder data
|
2018-08-19 11:45:46 +02:00
|
|
|
*/
|
2021-12-27 20:02:45 +01:00
|
|
|
void CodecVideoFlushBuffers(VideoDecoder *decoder) {
|
2018-08-19 11:45:46 +02:00
|
|
|
if (decoder->VideoCtx) {
|
2020-06-09 09:23:38 +02:00
|
|
|
avcodec_flush_buffers(decoder->VideoCtx);
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
2019-10-26 18:42:19 +02:00
|
|
|
// Audio
|
2018-08-19 11:45:46 +02:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
///
|
2019-10-26 18:42:19 +02:00
|
|
|
/// Audio decoder typedef.
|
2018-08-19 11:45:46 +02:00
|
|
|
///
|
|
|
|
typedef struct _audio_decoder_ AudioDecoder;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
///
|
2019-10-26 18:42:19 +02:00
|
|
|
/// Audio decoder structure.
|
2018-08-19 11:45:46 +02:00
|
|
|
///
|
2021-12-27 20:02:45 +01:00
|
|
|
struct _audio_decoder_ {
|
|
|
|
AVCodec *AudioCodec; ///< audio codec
|
|
|
|
AVCodecContext *AudioCtx; ///< audio codec context
|
2018-08-19 11:45:46 +02:00
|
|
|
|
2021-12-27 20:02:45 +01:00
|
|
|
char Passthrough; ///< current pass-through flags
|
|
|
|
int SampleRate; ///< current stream sample rate
|
|
|
|
int Channels; ///< current stream channels
|
2018-08-19 11:45:46 +02:00
|
|
|
|
2021-12-27 20:02:45 +01:00
|
|
|
int HwSampleRate; ///< hw sample rate
|
|
|
|
int HwChannels; ///< hw channels
|
2018-08-19 11:45:46 +02:00
|
|
|
|
2021-12-30 10:38:56 +01:00
|
|
|
AVFrame *Frame; ///< decoded audio frame buffer
|
2021-12-27 20:02:45 +01:00
|
|
|
SwrContext *Resample; ///< ffmpeg software resample context
|
2018-08-19 11:45:46 +02:00
|
|
|
|
2021-12-27 20:02:45 +01:00
|
|
|
uint16_t Spdif[24576 / 2]; ///< SPDIF output buffer
|
|
|
|
int SpdifIndex; ///< index into SPDIF output buffer
|
|
|
|
int SpdifCount; ///< SPDIF repeat counter
|
2018-08-19 11:45:46 +02:00
|
|
|
|
2021-12-27 20:02:45 +01:00
|
|
|
int64_t LastDelay; ///< last delay
|
|
|
|
struct timespec LastTime; ///< last time
|
|
|
|
int64_t LastPTS; ///< last PTS
|
2018-08-19 11:45:46 +02:00
|
|
|
|
2021-12-27 20:02:45 +01:00
|
|
|
int Drift; ///< accumulated audio drift
|
|
|
|
int DriftCorr; ///< audio drift correction value
|
|
|
|
int DriftFrac; ///< audio drift fraction for ac3
|
2018-08-19 11:45:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
///
|
2019-10-26 18:42:19 +02:00
|
|
|
/// IEC Data type enumeration.
|
2018-08-19 11:45:46 +02:00
|
|
|
///
|
2021-12-27 20:02:45 +01:00
|
|
|
enum IEC61937 {
|
|
|
|
IEC61937_AC3 = 0x01, ///< AC-3 data
|
2018-08-19 11:45:46 +02:00
|
|
|
// FIXME: more data types
|
2021-12-27 20:02:45 +01:00
|
|
|
IEC61937_EAC3 = 0x15, ///< E-AC-3 data
|
2018-08-19 11:45:46 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef USE_AUDIO_DRIFT_CORRECTION
|
2021-12-27 20:02:45 +01:00
|
|
|
#define CORRECT_PCM 1 ///< do PCM audio-drift correction
|
|
|
|
#define CORRECT_AC3 2 ///< do AC-3 audio-drift correction
|
|
|
|
static char CodecAudioDrift; ///< flag: enable audio-drift correction
|
2018-08-19 11:45:46 +02:00
|
|
|
#else
|
|
|
|
static const int CodecAudioDrift = 0;
|
|
|
|
#endif
|
|
|
|
#ifdef USE_PASSTHROUGH
|
2019-10-28 21:43:37 +01:00
|
|
|
///
|
|
|
|
/// Pass-through flags: CodecPCM, CodecAC3, CodecEAC3, ...
|
|
|
|
///
|
2018-08-19 11:45:46 +02:00
|
|
|
static char CodecPassthrough;
|
|
|
|
#else
|
|
|
|
static const int CodecPassthrough = 0;
|
|
|
|
#endif
|
2021-12-27 20:02:45 +01:00
|
|
|
static char CodecDownmix; ///< enable AC-3 decoder downmix
|
2018-08-19 11:45:46 +02:00
|
|
|
|
|
|
|
/**
|
2019-11-18 13:01:19 +01:00
|
|
|
** Allocate a new audio decoder context.
|
2018-08-19 11:45:46 +02:00
|
|
|
**
|
2019-11-18 13:01:19 +01:00
|
|
|
** @returns private decoder pointer for audio decoder.
|
2018-08-19 11:45:46 +02:00
|
|
|
*/
|
2021-12-27 20:02:45 +01:00
|
|
|
AudioDecoder *CodecAudioNewDecoder(void) {
|
2018-08-19 11:45:46 +02:00
|
|
|
AudioDecoder *audio_decoder;
|
|
|
|
|
|
|
|
if (!(audio_decoder = calloc(1, sizeof(*audio_decoder)))) {
|
2019-10-26 18:42:19 +02:00
|
|
|
Fatal(_("codec: can't allocate audio decoder\n"));
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
if (!(audio_decoder->Frame = av_frame_alloc())) {
|
2019-10-26 18:42:19 +02:00
|
|
|
Fatal(_("codec: can't allocate audio decoder frame buffer\n"));
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return audio_decoder;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-18 13:01:19 +01:00
|
|
|
** Deallocate an audio decoder context.
|
2018-08-19 11:45:46 +02:00
|
|
|
**
|
2019-11-18 13:01:19 +01:00
|
|
|
** @param decoder private audio decoder
|
2018-08-19 11:45:46 +02:00
|
|
|
*/
|
2021-12-27 20:02:45 +01:00
|
|
|
void CodecAudioDelDecoder(AudioDecoder *decoder) {
|
|
|
|
av_frame_free(&decoder->Frame); // callee does checks
|
2018-08-19 11:45:46 +02:00
|
|
|
free(decoder);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-18 13:01:19 +01:00
|
|
|
** Open audio decoder.
|
2018-08-19 11:45:46 +02:00
|
|
|
**
|
2019-11-18 13:01:19 +01:00
|
|
|
** @param audio_decoder private audio decoder
|
|
|
|
** @param codec_id audio codec id
|
2018-08-19 11:45:46 +02:00
|
|
|
*/
|
2021-12-27 20:02:45 +01:00
|
|
|
void CodecAudioOpen(AudioDecoder *audio_decoder, int codec_id) {
|
2018-08-19 11:45:46 +02:00
|
|
|
AVCodec *audio_codec;
|
|
|
|
|
2019-10-26 18:42:19 +02:00
|
|
|
Debug(3, "codec: using audio codec ID %#06x (%s)\n", codec_id, avcodec_get_name(codec_id));
|
2019-08-22 12:34:29 +02:00
|
|
|
if (!(audio_codec = avcodec_find_decoder(codec_id))) {
|
2019-10-28 21:43:37 +01:00
|
|
|
// if (!(audio_codec = avcodec_find_decoder(codec_id))) {
|
2019-10-26 18:42:19 +02:00
|
|
|
Fatal(_("codec: codec ID %#06x not found\n"), codec_id);
|
|
|
|
// FIXME: errors aren't fatal
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
audio_decoder->AudioCodec = audio_codec;
|
|
|
|
|
|
|
|
if (!(audio_decoder->AudioCtx = avcodec_alloc_context3(audio_codec))) {
|
2019-10-26 18:42:19 +02:00
|
|
|
Fatal(_("codec: can't allocate audio codec context\n"));
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (CodecDownmix) {
|
2021-01-26 09:17:39 +01:00
|
|
|
audio_decoder->AudioCtx->request_channel_layout = AV_CH_LAYOUT_STEREO;
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
pthread_mutex_lock(&CodecLockMutex);
|
|
|
|
// open codec
|
|
|
|
if (1) {
|
2019-10-26 18:42:19 +02:00
|
|
|
AVDictionary *av_dict;
|
|
|
|
|
|
|
|
av_dict = NULL;
|
|
|
|
// FIXME: import settings
|
2019-10-28 21:43:37 +01:00
|
|
|
// av_dict_set(&av_dict, "dmix_mode", "0", 0);
|
|
|
|
// av_dict_set(&av_dict, "ltrt_cmixlev", "1.414", 0);
|
|
|
|
// av_dict_set(&av_dict, "loro_cmixlev", "1.414", 0);
|
2019-10-26 18:42:19 +02:00
|
|
|
if (avcodec_open2(audio_decoder->AudioCtx, audio_codec, &av_dict) < 0) {
|
|
|
|
pthread_mutex_unlock(&CodecLockMutex);
|
|
|
|
Fatal(_("codec: can't open audio codec\n"));
|
|
|
|
}
|
|
|
|
av_dict_free(&av_dict);
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
pthread_mutex_unlock(&CodecLockMutex);
|
|
|
|
Debug(3, "codec: audio '%s'\n", audio_decoder->AudioCodec->long_name);
|
|
|
|
|
|
|
|
audio_decoder->SampleRate = 0;
|
|
|
|
audio_decoder->Channels = 0;
|
|
|
|
audio_decoder->HwSampleRate = 0;
|
|
|
|
audio_decoder->HwChannels = 0;
|
|
|
|
audio_decoder->LastDelay = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-18 13:01:19 +01:00
|
|
|
** Close audio decoder.
|
2018-08-19 11:45:46 +02:00
|
|
|
**
|
2019-11-18 13:01:19 +01:00
|
|
|
** @param audio_decoder private audio decoder
|
2018-08-19 11:45:46 +02:00
|
|
|
*/
|
2021-12-27 20:02:45 +01:00
|
|
|
void CodecAudioClose(AudioDecoder *audio_decoder) {
|
2018-08-19 11:45:46 +02:00
|
|
|
// FIXME: output any buffered data
|
2020-04-10 16:17:23 +02:00
|
|
|
|
2018-08-19 11:45:46 +02:00
|
|
|
if (audio_decoder->Resample) {
|
2019-10-26 18:42:19 +02:00
|
|
|
swr_free(&audio_decoder->Resample);
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
if (audio_decoder->AudioCtx) {
|
2019-10-26 18:42:19 +02:00
|
|
|
pthread_mutex_lock(&CodecLockMutex);
|
|
|
|
avcodec_close(audio_decoder->AudioCtx);
|
|
|
|
av_freep(&audio_decoder->AudioCtx);
|
|
|
|
pthread_mutex_unlock(&CodecLockMutex);
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-18 13:01:19 +01:00
|
|
|
** Set audio drift correction.
|
2018-08-19 11:45:46 +02:00
|
|
|
**
|
2019-11-18 13:01:19 +01:00
|
|
|
** @param mask enable mask (PCM, AC-3)
|
2018-08-19 11:45:46 +02:00
|
|
|
*/
|
2021-12-27 20:02:45 +01:00
|
|
|
void CodecSetAudioDrift(int mask) {
|
2018-08-19 11:45:46 +02:00
|
|
|
#ifdef USE_AUDIO_DRIFT_CORRECTION
|
|
|
|
CodecAudioDrift = mask & (CORRECT_PCM | CORRECT_AC3);
|
|
|
|
#endif
|
|
|
|
(void)mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-18 13:01:19 +01:00
|
|
|
** Set audio pass-through.
|
2018-08-19 11:45:46 +02:00
|
|
|
**
|
2019-11-18 13:01:19 +01:00
|
|
|
** @param mask enable mask (PCM, AC-3, E-AC-3)
|
2018-08-19 11:45:46 +02:00
|
|
|
*/
|
2021-12-27 20:02:45 +01:00
|
|
|
void CodecSetAudioPassthrough(int mask) {
|
2018-08-19 11:45:46 +02:00
|
|
|
#ifdef USE_PASSTHROUGH
|
|
|
|
CodecPassthrough = mask & (CodecPCM | CodecAC3 | CodecEAC3);
|
|
|
|
#endif
|
|
|
|
(void)mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-18 13:01:19 +01:00
|
|
|
** Set audio downmix.
|
2018-08-19 11:45:46 +02:00
|
|
|
**
|
2019-11-18 13:01:19 +01:00
|
|
|
** @param onoff enable/disable downmix.
|
2018-08-19 11:45:46 +02:00
|
|
|
*/
|
2021-12-27 20:02:45 +01:00
|
|
|
void CodecSetAudioDownmix(int onoff) {
|
2018-08-19 11:45:46 +02:00
|
|
|
if (onoff == -1) {
|
2019-10-26 18:42:19 +02:00
|
|
|
CodecDownmix ^= 1;
|
|
|
|
return;
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
CodecDownmix = onoff;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-18 13:01:19 +01:00
|
|
|
** Reorder audio frame.
|
2018-08-19 11:45:46 +02:00
|
|
|
**
|
2021-12-27 20:02:45 +01:00
|
|
|
** ffmpeg L R C Ls Rs -> alsa L R Ls Rs C
|
|
|
|
** ffmpeg L R C LFE Ls Rs -> alsa L R Ls Rs C LFE
|
|
|
|
** ffmpeg L R C LFE Ls Rs Rl Rr -> alsa L R Ls Rs C LFE Rl Rr
|
2018-08-19 11:45:46 +02:00
|
|
|
**
|
2021-12-27 20:02:45 +01:00
|
|
|
** @param buf[IN,OUT] sample buffer
|
|
|
|
** @param size size of sample buffer in bytes
|
|
|
|
** @param channels number of channels interleaved in sample buffer
|
2018-08-19 11:45:46 +02:00
|
|
|
*/
|
2021-12-27 20:02:45 +01:00
|
|
|
static void CodecReorderAudioFrame(int16_t *buf, int size, int channels) {
|
2018-08-19 11:45:46 +02:00
|
|
|
int i;
|
|
|
|
int c;
|
|
|
|
int ls;
|
|
|
|
int rs;
|
|
|
|
int lfe;
|
|
|
|
|
|
|
|
switch (channels) {
|
2019-10-26 18:42:19 +02:00
|
|
|
case 5:
|
|
|
|
size /= 2;
|
|
|
|
for (i = 0; i < size; i += 5) {
|
|
|
|
c = buf[i + 2];
|
|
|
|
ls = buf[i + 3];
|
|
|
|
rs = buf[i + 4];
|
|
|
|
buf[i + 2] = ls;
|
|
|
|
buf[i + 3] = rs;
|
|
|
|
buf[i + 4] = c;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
size /= 2;
|
|
|
|
for (i = 0; i < size; i += 6) {
|
|
|
|
c = buf[i + 2];
|
|
|
|
lfe = buf[i + 3];
|
|
|
|
ls = buf[i + 4];
|
|
|
|
rs = buf[i + 5];
|
|
|
|
buf[i + 2] = ls;
|
|
|
|
buf[i + 3] = rs;
|
|
|
|
buf[i + 4] = c;
|
|
|
|
buf[i + 5] = lfe;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
size /= 2;
|
|
|
|
for (i = 0; i < size; i += 8) {
|
|
|
|
c = buf[i + 2];
|
|
|
|
lfe = buf[i + 3];
|
|
|
|
ls = buf[i + 4];
|
|
|
|
rs = buf[i + 5];
|
|
|
|
buf[i + 2] = ls;
|
|
|
|
buf[i + 3] = rs;
|
|
|
|
buf[i + 4] = c;
|
|
|
|
buf[i + 5] = lfe;
|
|
|
|
}
|
|
|
|
break;
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-18 13:01:19 +01:00
|
|
|
** Handle audio format changes helper.
|
2018-08-19 11:45:46 +02:00
|
|
|
**
|
2019-11-18 13:01:19 +01:00
|
|
|
** @param audio_decoder audio decoder data
|
|
|
|
** @param[out] passthrough pass-through output
|
2018-08-19 11:45:46 +02:00
|
|
|
*/
|
2021-12-27 20:02:45 +01:00
|
|
|
static int CodecAudioUpdateHelper(AudioDecoder *audio_decoder, int *passthrough) {
|
2018-08-19 11:45:46 +02:00
|
|
|
const AVCodecContext *audio_ctx;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
audio_ctx = audio_decoder->AudioCtx;
|
2023-09-18 11:52:03 +02:00
|
|
|
Debug(3, "codec/audio: Chanlayout %lx format change %s %dHz *%d channels%s%s%s%s%s\n",audio_ctx->channel_layout,
|
2021-12-27 20:02:45 +01:00
|
|
|
av_get_sample_fmt_name(audio_ctx->sample_fmt), audio_ctx->sample_rate, audio_ctx->channels,
|
|
|
|
CodecPassthrough & CodecPCM ? " PCM" : "", CodecPassthrough & CodecMPA ? " MPA" : "",
|
|
|
|
CodecPassthrough & CodecAC3 ? " AC-3" : "", CodecPassthrough & CodecEAC3 ? " E-AC-3" : "",
|
|
|
|
CodecPassthrough ? " pass-through" : "");
|
2018-08-19 11:45:46 +02:00
|
|
|
|
|
|
|
*passthrough = 0;
|
|
|
|
audio_decoder->SampleRate = audio_ctx->sample_rate;
|
|
|
|
audio_decoder->HwSampleRate = audio_ctx->sample_rate;
|
|
|
|
audio_decoder->Channels = audio_ctx->channels;
|
|
|
|
audio_decoder->HwChannels = audio_ctx->channels;
|
|
|
|
audio_decoder->Passthrough = CodecPassthrough;
|
|
|
|
|
|
|
|
// SPDIF/HDMI pass-through
|
2021-12-27 20:02:45 +01:00
|
|
|
if ((CodecPassthrough & CodecAC3 && audio_ctx->codec_id == AV_CODEC_ID_AC3) ||
|
|
|
|
(CodecPassthrough & CodecEAC3 && audio_ctx->codec_id == AV_CODEC_ID_EAC3)) {
|
2019-10-26 18:42:19 +02:00
|
|
|
if (audio_ctx->codec_id == AV_CODEC_ID_EAC3) {
|
|
|
|
// E-AC-3 over HDMI some receivers need HBR
|
2023-07-14 09:15:34 +02:00
|
|
|
//audio_decoder->HwSampleRate *= 4;
|
2019-10-26 18:42:19 +02:00
|
|
|
}
|
|
|
|
audio_decoder->HwChannels = 2;
|
2021-12-27 20:02:45 +01:00
|
|
|
audio_decoder->SpdifIndex = 0; // reset buffer
|
2019-10-26 18:42:19 +02:00
|
|
|
audio_decoder->SpdifCount = 0;
|
|
|
|
*passthrough = 1;
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
// channels/sample-rate not support?
|
2019-10-26 18:42:19 +02:00
|
|
|
if ((err = AudioSetup(&audio_decoder->HwSampleRate, &audio_decoder->HwChannels, *passthrough))) {
|
|
|
|
|
|
|
|
// try E-AC-3 none HBR
|
|
|
|
audio_decoder->HwSampleRate /= 4;
|
2021-12-27 20:02:45 +01:00
|
|
|
if (audio_ctx->codec_id != AV_CODEC_ID_EAC3 ||
|
|
|
|
(err = AudioSetup(&audio_decoder->HwSampleRate, &audio_decoder->HwChannels, *passthrough))) {
|
2019-10-26 18:42:19 +02:00
|
|
|
|
|
|
|
Debug(3, "codec/audio: audio setup error\n");
|
|
|
|
// FIXME: handle errors
|
|
|
|
audio_decoder->HwChannels = 0;
|
|
|
|
audio_decoder->HwSampleRate = 0;
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Debug(3, "codec/audio: resample %s %dHz *%d -> %s %dHz *%d\n", av_get_sample_fmt_name(audio_ctx->sample_fmt),
|
2021-12-27 20:02:45 +01:00
|
|
|
audio_ctx->sample_rate, audio_ctx->channels, av_get_sample_fmt_name(AV_SAMPLE_FMT_S16),
|
|
|
|
audio_decoder->HwSampleRate, audio_decoder->HwChannels);
|
2018-08-19 11:45:46 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-18 13:01:19 +01:00
|
|
|
** Audio pass-through decoder helper.
|
2018-08-19 11:45:46 +02:00
|
|
|
**
|
2019-11-18 13:01:19 +01:00
|
|
|
** @param audio_decoder audio decoder data
|
2021-12-27 20:02:45 +01:00
|
|
|
** @param avpkt undecoded audio packet
|
2018-08-19 11:45:46 +02:00
|
|
|
*/
|
2021-12-27 20:02:45 +01:00
|
|
|
static int CodecAudioPassthroughHelper(AudioDecoder *audio_decoder, const AVPacket *avpkt) {
|
2018-08-19 11:45:46 +02:00
|
|
|
#ifdef USE_PASSTHROUGH
|
|
|
|
const AVCodecContext *audio_ctx;
|
|
|
|
|
|
|
|
audio_ctx = audio_decoder->AudioCtx;
|
|
|
|
// SPDIF/HDMI passthrough
|
|
|
|
if (CodecPassthrough & CodecAC3 && audio_ctx->codec_id == AV_CODEC_ID_AC3) {
|
2019-10-26 18:42:19 +02:00
|
|
|
uint16_t *spdif;
|
|
|
|
int spdif_sz;
|
2018-08-19 11:45:46 +02:00
|
|
|
|
2019-10-26 18:42:19 +02:00
|
|
|
spdif = audio_decoder->Spdif;
|
|
|
|
spdif_sz = 6144;
|
2018-08-19 11:45:46 +02:00
|
|
|
|
|
|
|
#ifdef USE_AC3_DRIFT_CORRECTION
|
2019-10-26 18:42:19 +02:00
|
|
|
// FIXME: this works with some TVs/AVReceivers
|
|
|
|
// FIXME: write burst size drift correction, which should work with all
|
|
|
|
if (CodecAudioDrift & CORRECT_AC3) {
|
|
|
|
int x;
|
|
|
|
|
2021-12-27 20:02:45 +01:00
|
|
|
x = (audio_decoder->DriftFrac + (audio_decoder->DriftCorr * spdif_sz)) /
|
|
|
|
(10 * audio_decoder->HwSampleRate * 100);
|
|
|
|
audio_decoder->DriftFrac = (audio_decoder->DriftFrac + (audio_decoder->DriftCorr * spdif_sz)) %
|
|
|
|
(10 * audio_decoder->HwSampleRate * 100);
|
2019-10-26 18:42:19 +02:00
|
|
|
// round to word border
|
|
|
|
x *= audio_decoder->HwChannels * 4;
|
2021-12-27 20:02:45 +01:00
|
|
|
if (x < -64) { // limit correction
|
2019-10-26 18:42:19 +02:00
|
|
|
x = -64;
|
|
|
|
} else if (x > 64) {
|
|
|
|
x = 64;
|
|
|
|
}
|
|
|
|
spdif_sz += x;
|
|
|
|
}
|
2018-08-19 11:45:46 +02:00
|
|
|
#endif
|
|
|
|
|
2019-10-26 18:42:19 +02:00
|
|
|
// build SPDIF header and append A52 audio to it
|
|
|
|
// avpkt is the original data
|
|
|
|
if (spdif_sz < avpkt->size + 8) {
|
|
|
|
Error(_("codec/audio: decoded data smaller than encoded\n"));
|
|
|
|
return -1;
|
|
|
|
}
|
2021-12-27 20:02:45 +01:00
|
|
|
spdif[0] = htole16(0xF872); // iec 61937 sync word
|
2019-10-26 18:42:19 +02:00
|
|
|
spdif[1] = htole16(0x4E1F);
|
|
|
|
spdif[2] = htole16(IEC61937_AC3 | (avpkt->data[5] & 0x07) << 8);
|
|
|
|
spdif[3] = htole16(avpkt->size * 8);
|
|
|
|
// copy original data for output
|
|
|
|
// FIXME: not 100% sure, if endian is correct on not intel hardware
|
|
|
|
swab(avpkt->data, spdif + 4, avpkt->size);
|
|
|
|
// FIXME: don't need to clear always
|
|
|
|
memset(spdif + 4 + avpkt->size / 2, 0, spdif_sz - 8 - avpkt->size);
|
|
|
|
// don't play with the ac-3 samples
|
|
|
|
AudioEnqueue(spdif, spdif_sz);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (CodecPassthrough & CodecEAC3 && audio_ctx->codec_id == AV_CODEC_ID_EAC3) {
|
|
|
|
uint16_t *spdif;
|
|
|
|
int spdif_sz;
|
|
|
|
int repeat;
|
|
|
|
|
|
|
|
// build SPDIF header and append A52 audio to it
|
|
|
|
// avpkt is the original data
|
|
|
|
spdif = audio_decoder->Spdif;
|
2021-12-27 20:02:45 +01:00
|
|
|
spdif_sz = 24576; // 4 * 6144
|
2019-10-26 18:42:19 +02:00
|
|
|
if (audio_decoder->HwSampleRate == 48000) {
|
|
|
|
spdif_sz = 6144;
|
|
|
|
}
|
|
|
|
if (spdif_sz < audio_decoder->SpdifIndex + avpkt->size + 8) {
|
|
|
|
Error(_("codec/audio: decoded data smaller than encoded\n"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
// check if we must pack multiple packets
|
|
|
|
repeat = 1;
|
2021-12-27 20:02:45 +01:00
|
|
|
if ((avpkt->data[4] & 0xc0) != 0xc0) { // fscod
|
|
|
|
static const uint8_t eac3_repeat[4] = {6, 3, 2, 1};
|
2019-10-26 18:42:19 +02:00
|
|
|
|
|
|
|
// fscod2
|
|
|
|
repeat = eac3_repeat[(avpkt->data[4] & 0x30) >> 4];
|
|
|
|
}
|
|
|
|
// fprintf(stderr, "repeat %d %d\n", repeat, avpkt->size);
|
|
|
|
|
|
|
|
// copy original data for output
|
|
|
|
// pack upto repeat EAC-3 pakets into one IEC 61937 burst
|
|
|
|
// FIXME: not 100% sure, if endian is correct on not intel hardware
|
|
|
|
swab(avpkt->data, spdif + 4 + audio_decoder->SpdifIndex, avpkt->size);
|
|
|
|
audio_decoder->SpdifIndex += avpkt->size;
|
|
|
|
if (++audio_decoder->SpdifCount < repeat) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-12-27 20:02:45 +01:00
|
|
|
spdif[0] = htole16(0xF872); // iec 61937 sync word
|
2019-10-26 18:42:19 +02:00
|
|
|
spdif[1] = htole16(0x4E1F);
|
|
|
|
spdif[2] = htole16(IEC61937_EAC3);
|
|
|
|
spdif[3] = htole16(audio_decoder->SpdifIndex * 8);
|
|
|
|
memset(spdif + 4 + audio_decoder->SpdifIndex / 2, 0, spdif_sz - 8 - audio_decoder->SpdifIndex);
|
|
|
|
|
|
|
|
// don't play with the eac-3 samples
|
|
|
|
AudioEnqueue(spdif, spdif_sz);
|
|
|
|
|
|
|
|
audio_decoder->SpdifIndex = 0;
|
|
|
|
audio_decoder->SpdifCount = 0;
|
|
|
|
return 1;
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-18 13:01:19 +01:00
|
|
|
** Set/update audio pts clock.
|
2018-08-19 11:45:46 +02:00
|
|
|
**
|
2019-11-18 13:01:19 +01:00
|
|
|
** @param audio_decoder audio decoder data
|
2021-12-27 20:02:45 +01:00
|
|
|
** @param pts presentation timestamp
|
2018-08-19 11:45:46 +02:00
|
|
|
*/
|
2021-12-27 20:02:45 +01:00
|
|
|
static void CodecAudioSetClock(AudioDecoder *audio_decoder, int64_t pts) {
|
2018-08-19 11:45:46 +02:00
|
|
|
#ifdef USE_AUDIO_DRIFT_CORRECTION
|
|
|
|
struct timespec nowtime;
|
|
|
|
int64_t delay;
|
|
|
|
int64_t tim_diff;
|
|
|
|
int64_t pts_diff;
|
|
|
|
int drift;
|
|
|
|
int corr;
|
|
|
|
|
|
|
|
AudioSetClock(pts);
|
|
|
|
|
|
|
|
delay = AudioGetDelay();
|
|
|
|
if (!delay) {
|
2019-10-26 18:42:19 +02:00
|
|
|
return;
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &nowtime);
|
|
|
|
if (!audio_decoder->LastDelay) {
|
2019-10-26 18:42:19 +02:00
|
|
|
audio_decoder->LastTime = nowtime;
|
|
|
|
audio_decoder->LastPTS = pts;
|
|
|
|
audio_decoder->LastDelay = delay;
|
|
|
|
audio_decoder->Drift = 0;
|
|
|
|
audio_decoder->DriftFrac = 0;
|
|
|
|
Debug(3, "codec/audio: inital drift delay %" PRId64 "ms\n", delay / 90);
|
|
|
|
return;
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
// collect over some time
|
|
|
|
pts_diff = pts - audio_decoder->LastPTS;
|
|
|
|
if (pts_diff < 10 * 1000 * 90) {
|
2019-10-26 18:42:19 +02:00
|
|
|
return;
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
|
2021-12-27 20:02:45 +01:00
|
|
|
tim_diff = (nowtime.tv_sec - audio_decoder->LastTime.tv_sec) * 1000 * 1000 * 1000 +
|
|
|
|
(nowtime.tv_nsec - audio_decoder->LastTime.tv_nsec);
|
2018-08-19 11:45:46 +02:00
|
|
|
|
2019-10-26 18:42:19 +02:00
|
|
|
drift = (tim_diff * 90) / (1000 * 1000) - pts_diff + delay - audio_decoder->LastDelay;
|
2018-08-19 11:45:46 +02:00
|
|
|
|
|
|
|
// adjust rounding error
|
|
|
|
nowtime.tv_nsec -= nowtime.tv_nsec % (1000 * 1000 / 90);
|
|
|
|
audio_decoder->LastTime = nowtime;
|
|
|
|
audio_decoder->LastPTS = pts;
|
|
|
|
audio_decoder->LastDelay = delay;
|
|
|
|
|
|
|
|
if (0) {
|
2019-10-26 18:42:19 +02:00
|
|
|
Debug(3, "codec/audio: interval P:%5" PRId64 "ms T:%5" PRId64 "ms D:%4" PRId64 "ms %f %d\n", pts_diff / 90,
|
2021-12-27 20:02:45 +01:00
|
|
|
tim_diff / (1000 * 1000), delay / 90, drift / 90.0, audio_decoder->DriftCorr);
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
// underruns and av_resample have the same time :(((
|
|
|
|
if (abs(drift) > 10 * 90) {
|
2019-10-26 18:42:19 +02:00
|
|
|
// drift too big, pts changed?
|
|
|
|
Debug(3, "codec/audio: drift(%6d) %3dms reset\n", audio_decoder->DriftCorr, drift / 90);
|
|
|
|
audio_decoder->LastDelay = 0;
|
2018-08-19 11:45:46 +02:00
|
|
|
#ifdef DEBUG
|
2021-12-27 20:02:45 +01:00
|
|
|
corr = 0; // keep gcc happy
|
2018-08-19 11:45:46 +02:00
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
|
2019-10-26 18:42:19 +02:00
|
|
|
drift += audio_decoder->Drift;
|
|
|
|
audio_decoder->Drift = drift;
|
|
|
|
corr = (10 * audio_decoder->HwSampleRate * drift) / (90 * 1000);
|
|
|
|
// SPDIF/HDMI passthrough
|
2021-12-27 20:02:45 +01:00
|
|
|
if ((CodecAudioDrift & CORRECT_AC3) &&
|
|
|
|
(!(CodecPassthrough & CodecAC3) || audio_decoder->AudioCtx->codec_id != AV_CODEC_ID_AC3) &&
|
|
|
|
(!(CodecPassthrough & CodecEAC3) || audio_decoder->AudioCtx->codec_id != AV_CODEC_ID_EAC3)) {
|
2019-10-26 18:42:19 +02:00
|
|
|
audio_decoder->DriftCorr = -corr;
|
|
|
|
}
|
2018-08-19 11:45:46 +02:00
|
|
|
|
2021-12-27 20:02:45 +01:00
|
|
|
if (audio_decoder->DriftCorr < -20000) { // limit correction
|
2019-10-26 18:42:19 +02:00
|
|
|
audio_decoder->DriftCorr = -20000;
|
|
|
|
} else if (audio_decoder->DriftCorr > 20000) {
|
|
|
|
audio_decoder->DriftCorr = 20000;
|
|
|
|
}
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (audio_decoder->Resample && audio_decoder->DriftCorr) {
|
2019-10-26 18:42:19 +02:00
|
|
|
int distance;
|
|
|
|
|
|
|
|
// try workaround for buggy ffmpeg 0.10
|
|
|
|
if (abs(audio_decoder->DriftCorr) < 2000) {
|
|
|
|
distance = (pts_diff * audio_decoder->HwSampleRate) / (900 * 1000);
|
|
|
|
} else {
|
|
|
|
distance = (pts_diff * audio_decoder->HwSampleRate) / (90 * 1000);
|
|
|
|
}
|
|
|
|
if (swr_set_compensation(audio_decoder->Resample, audio_decoder->DriftCorr / 10, distance)) {
|
|
|
|
Debug(3, "codec/audio: swr_set_compensation failed\n");
|
|
|
|
}
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
if (1) {
|
2019-10-26 18:42:19 +02:00
|
|
|
static int c;
|
2018-08-19 11:45:46 +02:00
|
|
|
|
2019-10-26 18:42:19 +02:00
|
|
|
if (!(c++ % 10)) {
|
|
|
|
Debug(3, "codec/audio: drift(%6d) %8dus %5d\n", audio_decoder->DriftCorr, drift * 1000 / 90, corr);
|
|
|
|
}
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
AudioSetClock(pts);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-18 13:01:19 +01:00
|
|
|
** Handle audio format changes.
|
2018-08-19 11:45:46 +02:00
|
|
|
**
|
2019-11-18 13:01:19 +01:00
|
|
|
** @param audio_decoder audio decoder data
|
2018-08-19 11:45:46 +02:00
|
|
|
*/
|
2021-12-27 20:02:45 +01:00
|
|
|
static void CodecAudioUpdateFormat(AudioDecoder *audio_decoder) {
|
2018-08-19 11:45:46 +02:00
|
|
|
int passthrough;
|
|
|
|
const AVCodecContext *audio_ctx;
|
|
|
|
|
|
|
|
if (CodecAudioUpdateHelper(audio_decoder, &passthrough)) {
|
2019-10-26 18:42:19 +02:00
|
|
|
// FIXME: handle swresample format conversions.
|
|
|
|
return;
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
2021-12-27 20:02:45 +01:00
|
|
|
if (passthrough) { // pass-through no conversion allowed
|
2019-10-26 18:42:19 +02:00
|
|
|
return;
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
audio_ctx = audio_decoder->AudioCtx;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
2021-12-27 20:02:45 +01:00
|
|
|
if (audio_ctx->sample_fmt == AV_SAMPLE_FMT_S16 && audio_ctx->sample_rate == audio_decoder->HwSampleRate &&
|
|
|
|
!CodecAudioDrift) {
|
2019-10-26 18:42:19 +02:00
|
|
|
// FIXME: use Resample only, when it is needed!
|
|
|
|
fprintf(stderr, "no resample needed\n");
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-12-27 20:02:45 +01:00
|
|
|
audio_decoder->Resample = swr_alloc_set_opts(audio_decoder->Resample, audio_ctx->channel_layout, AV_SAMPLE_FMT_S16,
|
|
|
|
audio_decoder->HwSampleRate, audio_ctx->channel_layout,
|
|
|
|
audio_ctx->sample_fmt, audio_ctx->sample_rate, 0, NULL);
|
2018-08-19 11:45:46 +02:00
|
|
|
if (audio_decoder->Resample) {
|
2019-10-26 18:42:19 +02:00
|
|
|
swr_init(audio_decoder->Resample);
|
2018-08-19 11:45:46 +02:00
|
|
|
} else {
|
2019-10-26 18:42:19 +02:00
|
|
|
Error(_("codec/audio: can't setup resample\n"));
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-18 13:01:19 +01:00
|
|
|
** Decode an audio packet.
|
2018-08-19 11:45:46 +02:00
|
|
|
**
|
2019-11-18 13:01:19 +01:00
|
|
|
** PTS must be handled self.
|
2018-08-19 11:45:46 +02:00
|
|
|
**
|
2019-11-18 13:01:19 +01:00
|
|
|
** @note the caller has not aligned avpkt and not cleared the end.
|
2018-08-19 11:45:46 +02:00
|
|
|
**
|
2019-11-18 13:01:19 +01:00
|
|
|
** @param audio_decoder audio decoder data
|
2021-12-27 20:02:45 +01:00
|
|
|
** @param avpkt audio packet
|
2018-08-19 11:45:46 +02:00
|
|
|
*/
|
2019-01-04 12:27:13 +01:00
|
|
|
|
2021-12-27 20:02:45 +01:00
|
|
|
void CodecAudioDecode(AudioDecoder *audio_decoder, const AVPacket *avpkt) {
|
2019-01-04 12:27:13 +01:00
|
|
|
AVCodecContext *audio_ctx = audio_decoder->AudioCtx;
|
2018-08-19 11:45:46 +02:00
|
|
|
|
2019-01-04 12:27:13 +01:00
|
|
|
if (audio_ctx->codec_type == AVMEDIA_TYPE_AUDIO) {
|
2019-10-26 18:42:19 +02:00
|
|
|
int ret;
|
|
|
|
AVPacket pkt[1];
|
|
|
|
AVFrame *frame = audio_decoder->Frame;
|
|
|
|
|
|
|
|
av_frame_unref(frame);
|
2021-12-27 20:02:45 +01:00
|
|
|
*pkt = *avpkt; // use copy
|
2019-10-26 18:42:19 +02:00
|
|
|
ret = avcodec_send_packet(audio_ctx, pkt);
|
|
|
|
if (ret < 0) {
|
|
|
|
Debug(3, "codec: sending audio packet failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
ret = avcodec_receive_frame(audio_ctx, frame);
|
|
|
|
if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
|
|
|
|
Debug(3, "codec: receiving audio frame failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret >= 0) {
|
|
|
|
// update audio clock
|
2021-12-27 20:02:45 +01:00
|
|
|
if (avpkt->pts != (int64_t)AV_NOPTS_VALUE) {
|
2019-10-26 18:42:19 +02:00
|
|
|
CodecAudioSetClock(audio_decoder, avpkt->pts);
|
|
|
|
}
|
|
|
|
// format change
|
2021-12-27 20:02:45 +01:00
|
|
|
if (audio_decoder->Passthrough != CodecPassthrough ||
|
|
|
|
audio_decoder->SampleRate != audio_ctx->sample_rate ||
|
|
|
|
audio_decoder->Channels != audio_ctx->channels) {
|
2019-10-26 18:42:19 +02:00
|
|
|
CodecAudioUpdateFormat(audio_decoder);
|
|
|
|
}
|
|
|
|
if (!audio_decoder->HwSampleRate || !audio_decoder->HwChannels) {
|
2021-12-27 20:02:45 +01:00
|
|
|
return; // unsupported sample format
|
2019-10-26 18:42:19 +02:00
|
|
|
}
|
|
|
|
if (CodecAudioPassthroughHelper(audio_decoder, avpkt)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (audio_decoder->Resample) {
|
|
|
|
uint8_t outbuf[8192 * 2 * 8];
|
|
|
|
uint8_t *out[1];
|
2020-04-10 16:17:23 +02:00
|
|
|
|
2019-10-26 18:42:19 +02:00
|
|
|
out[0] = outbuf;
|
2021-12-27 20:02:45 +01:00
|
|
|
ret = swr_convert(audio_decoder->Resample, out, sizeof(outbuf) / (2 * audio_decoder->HwChannels),
|
|
|
|
(const uint8_t **)frame->extended_data, frame->nb_samples);
|
2019-10-26 18:42:19 +02:00
|
|
|
if (ret > 0) {
|
|
|
|
if (!(audio_decoder->Passthrough & CodecPCM)) {
|
2021-12-27 20:02:45 +01:00
|
|
|
CodecReorderAudioFrame((int16_t *)outbuf, ret * 2 * audio_decoder->HwChannels,
|
|
|
|
audio_decoder->HwChannels);
|
2019-10-26 18:42:19 +02:00
|
|
|
}
|
|
|
|
AudioEnqueue(outbuf, ret * 2 * audio_decoder->HwChannels);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-08-19 11:45:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-18 13:01:19 +01:00
|
|
|
** Flush the audio decoder.
|
2018-08-19 11:45:46 +02:00
|
|
|
**
|
2019-11-18 13:01:19 +01:00
|
|
|
** @param decoder audio decoder data
|
2018-08-19 11:45:46 +02:00
|
|
|
*/
|
2021-12-27 20:02:45 +01:00
|
|
|
void CodecAudioFlushBuffers(AudioDecoder *decoder) { avcodec_flush_buffers(decoder->AudioCtx); }
|
2018-08-19 11:45:46 +02:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
2019-10-26 18:42:19 +02:00
|
|
|
// Codec
|
2018-08-19 11:45:46 +02:00
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
/**
|
2019-11-18 13:01:19 +01:00
|
|
|
** Empty log callback
|
2018-08-19 11:45:46 +02:00
|
|
|
*/
|
2021-12-27 20:02:45 +01:00
|
|
|
static void CodecNoopCallback(__attribute__((unused)) void *ptr, __attribute__((unused)) int level,
|
|
|
|
__attribute__((unused)) const char *fmt, __attribute__((unused)) va_list vl) {}
|
2018-08-19 11:45:46 +02:00
|
|
|
|
|
|
|
/**
|
2019-11-18 13:01:19 +01:00
|
|
|
** Codec init
|
2018-08-19 11:45:46 +02:00
|
|
|
*/
|
2021-12-27 20:02:45 +01:00
|
|
|
void CodecInit(void) {
|
2018-08-19 11:45:46 +02:00
|
|
|
pthread_mutex_init(&CodecLockMutex, NULL);
|
|
|
|
#ifndef DEBUG
|
|
|
|
// disable display ffmpeg error messages
|
|
|
|
av_log_set_callback(CodecNoopCallback);
|
|
|
|
#else
|
|
|
|
(void)CodecNoopCallback;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-18 13:01:19 +01:00
|
|
|
** Codec exit.
|
2018-08-19 11:45:46 +02:00
|
|
|
*/
|
2021-12-27 20:02:45 +01:00
|
|
|
void CodecExit(void) { pthread_mutex_destroy(&CodecLockMutex); }
|