forked from zillevdr/vdr-plugin-softhddevice-drm
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcodec_video.cpp
More file actions
573 lines (490 loc) · 17.3 KB
/
Copy pathcodec_video.cpp
File metadata and controls
573 lines (490 loc) · 17.3 KB
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
// SPDX-License-Identifier: AGPL-3.0-or-later
/**
* @file codec_video.cpp
* Video Decoder
*
* This file defines cVideoDecoder, which has all the functions
* to decode video data. It's the video interface to ffmpeg.
*
* @copyright 2009 - 2015 by Johns. All Rights Reserved.
* @copyright 2018 by zille. All Rights Reserved.
* @copyright 2025 - 2026 by Andreas Baierl. All Rights Reserved.
*
* @license{AGPL-3.0-or-later}
*/
#include <mutex>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavcodec/bsf.h>
//#include <libavutil/opt.h>
#include <libavutil/pixdesc.h>
}
#include "codec_video.h"
#include "logger.h"
#include "misc.h"
//#define NUM_CAPTURE_BUFFERS 10
//#define NUM_OUTPUT_BUFFERS 10
/********************************************************************************
* Static functions
*******************************************************************************/
/**
* Callback to negotiate the PixelFormat
*
* @param videoCtx video codec context
* @param fmt 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
*
* @return the negotiated pixel format
*
* @ingroup videodecoder
*/
static enum AVPixelFormat GetFormat(AVCodecContext * videoCtx,
const enum AVPixelFormat *fmt)
{
while (*fmt != AV_PIX_FMT_NONE) {
LOGDEBUG2(L_CODEC, "videocodec: %s: PixelFormat: %s videoCtx->pix_fmt: %s sw_pix_fmt: %s Codecname: %s",
__FUNCTION__,
av_get_pix_fmt_name(*fmt), av_get_pix_fmt_name(videoCtx->pix_fmt),
av_get_pix_fmt_name(videoCtx->sw_pix_fmt), videoCtx->codec->name);
if (*fmt == AV_PIX_FMT_DRM_PRIME) {
return AV_PIX_FMT_DRM_PRIME;
}
if (*fmt == AV_PIX_FMT_YUV420P) {
return AV_PIX_FMT_YUV420P;
}
fmt++;
}
LOGWARNING("videocodec: %s: No pixel format found! Set default format.", __FUNCTION__);
return avcodec_default_get_format(videoCtx, fmt);
}
/**
* Find a hardware based video decoder config
*
* @param codec codec for which we should find a hw config
*
* @return AVCodecHWConfig if found, NULL otherwise
*
* @ingroup videodecoder
*/
static const AVCodecHWConfig *FindHWConfig(const AVCodec *codec)
{
const AVCodecHWConfig *config = NULL;
for (int n = 0; (config = avcodec_get_hw_config(codec, n)); n++)
{
if (!(config->pix_fmt == AV_PIX_FMT_DRM_PRIME))
continue;
if ((config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX) ||
(config->methods & AV_CODEC_HW_CONFIG_METHOD_INTERNAL))
return config;
}
return NULL;
}
/**
* Find a suitable video codec (hardware decoding)
*
* @param codecId video codec id
*
* @return AVCodec if found, NULL otherwise
*
* @ingroup videodecoder
*/
static const AVCodec *FindHWDecoder(enum AVCodecID codecId)
{
const AVCodec *codec;
void *i = 0;
while ((codec = av_codec_iterate(&i))) {
if (!av_codec_is_decoder(codec))
continue;
if (codec->id != codecId)
continue;
const AVCodecHWConfig *config = FindHWConfig(codec);
if (config)
return codec;
}
return NULL;
}
/**
* Find a suitable video codec (software decoding)
*
* @param codecId video codec id
*
* @return AVCodec if found, NULL otherwise
*
* @ingroup videodecoder
*/
static const AVCodec *FindSWDecoder(enum AVCodecID codecId)
{
return avcodec_find_decoder(codecId);
}
/**
* Create a new video decoder
*
* @param identifier string to identify decoder for video or pip stream
* (used within logging only)
*/
cVideoDecoder::cVideoDecoder(const char *identifier)
: m_identifier(identifier)
{
av_log_set_callback(cSoftHdLogger::LogFFmpegCallback);
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58,18,100)
avcodec_register_all(); // register all formats and codecs
#endif
}
/**
* Open the video decoder
*
* @param codecId video codec id
* @param par codec parameters
* @param timebase timebase
* @param forceSoftwareDecoder force software decoding
* @param width force width (only for H264 and if par is not set)
* @param height force height (only for H264 and if par is not set)
*
* @retval 0 decoder successfully opend
* @retval -1 opening the decoder failed
*/
int cVideoDecoder::Open(enum AVCodecID codecId, AVCodecParameters * par,
AVRational timebase, bool forceSoftwareDecoder,
int width, int height)
{
std::unique_lock<std::mutex> lock(m_mutex);
if (m_pVideoCtx != nullptr)
return 0;
const AVCodec *codec = nullptr;
m_isHardwareDecoder = false;
LOGDEBUG2(L_CODEC, "videocodec: %s: %s: Try to open decoder for codec \"%s\"%s", m_identifier, __FUNCTION__,
avcodec_get_name(codecId), forceSoftwareDecoder ? " (sw decoding forced)" : "");
if (!forceSoftwareDecoder)
codec = FindHWDecoder(codecId);
if (codec) {
m_isHardwareDecoder = true;
} else {
if (!forceSoftwareDecoder)
LOGDEBUG2(L_CODEC, "videocodec: %s: no HW decoder found for codec \"%s\", try software decoder%s", __FUNCTION__, avcodec_get_name(codecId), forceSoftwareDecoder ? " (forced)" : "");
codec = FindSWDecoder(codecId);
}
if (!codec) {
LOGERROR("videocodec: %s: %s: Could not find any decoder for codec \"%s\"!", m_identifier, __FUNCTION__, avcodec_get_name(codecId));
return -1;
}
m_pVideoCtx = avcodec_alloc_context3(codec);
if (!m_pVideoCtx) {
LOGERROR("videocodec: %s: %s: can't alloc codec context!", m_identifier, __FUNCTION__);
return -1;
}
const AVCodecHWConfig *config = m_isHardwareDecoder ? FindHWConfig(codec) : NULL;
static AVBufferRef *hwDeviceCtx = NULL;
if (config && (config->methods & AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX)) {
const char *type_name = av_hwdevice_get_type_name(config->device_type);
if (av_hwdevice_ctx_create(&hwDeviceCtx, config->device_type, NULL, NULL, 0) < 0) {
avcodec_free_context(&m_pVideoCtx);
LOGERROR("videocodec: %s: %s: Error creating HW context %s", m_identifier, __FUNCTION__,
type_name ? type_name : "unknown");
return -1;
}
m_pVideoCtx->hw_device_ctx = hwDeviceCtx;
m_pVideoCtx->pix_fmt = AV_PIX_FMT_DRM_PRIME;
}
if (par && avcodec_parameters_to_context(m_pVideoCtx, par) < 0)
LOGERROR("videocodec: %s: %s: insert parameters to context failed!", m_identifier, __FUNCTION__);
m_pVideoCtx->codec_id = codecId;
m_pVideoCtx->get_format = GetFormat;
m_pVideoCtx->opaque = this;
m_pVideoCtx->pkt_timebase.num = 1;
m_pVideoCtx->pkt_timebase.den = 90000;
if (av_q2d(timebase) > 0)
m_pVideoCtx->pkt_timebase = timebase;
if (codecId == AV_CODEC_ID_H264) {
if (par) {
m_pVideoCtx->coded_width = par->width;
m_pVideoCtx->coded_height = par->height;
m_pVideoCtx->width = par->width;
m_pVideoCtx->height = par->height;
LOGDEBUG2(L_CODEC, "videocodec: %s: %s: Set width %d and height %d from par", m_identifier, __FUNCTION__, par->width, par->height);
} else if (width && height) {
m_pVideoCtx->coded_width = width;
m_pVideoCtx->coded_height = height;
m_pVideoCtx->width = width;
m_pVideoCtx->height = height;
LOGDEBUG2(L_CODEC, "videocodec: %s: %s: Set width %d and height %d forced", m_identifier, __FUNCTION__, width, height);
}
}
if (codec->capabilities & (AV_CODEC_CAP_FRAME_THREADS | AV_CODEC_CAP_SLICE_THREADS))
m_pVideoCtx->thread_count = !m_isHardwareDecoder ? 4 : 1;
if (codec->capabilities & AV_CODEC_CAP_SLICE_THREADS)
m_pVideoCtx->thread_type = FF_THREAD_SLICE;
/*
if (strstr(codec->name, "_v4l2")) {
if (av_opt_set_int(m_pVideoCtx->priv_data, "num_capture_buffers", NUM_CAPTURE_BUFFERS, 0) < 0) {
LOGERROR("videocodec: %s: can't set %d num_capture_buffers", __FUNCTION__, NUM_CAPTURE_BUFFERS);
}
LOGDEBUG2(L_CODEC, "cVideoDecoder::Open: set num_capture_buffers %d", NUM_CAPTURE_BUFFERS);
if (av_opt_set_int(m_pVideoCtx->priv_data, "num_output_buffers", NUM_OUTPUT_BUFFERS, 0) < 0) {
LOGERROR("videocodec: %s: can't set %d num_output_buffers", __FUNCTION__, NUM_OUTPUT_BUFFERS);
}
LOGDEBUG2(L_CODEC, "videocodec: %s: set num_output_buffers %d", __FUNCTION__, NUM_OUTPUT_BUFFERS);
}
*/
int err = avcodec_open2(m_pVideoCtx, m_pVideoCtx->codec, NULL);
if (err < 0) {
avcodec_free_context(&m_pVideoCtx);
if (!m_isHardwareDecoder) {
LOGERROR("videocodec: %s: %s: Error opening the decoder: %s", m_identifier, __FUNCTION__, av_err2str(err));
return -1;
}
LOGDEBUG2(L_CODEC, "videocodec: %s: %s: Could not open hw decoder \"%s\", force using software decoder",
m_identifier, __FUNCTION__, codec->long_name ? codec->long_name : codec->name);
// unlock here, otherwise we run into a deadlock
lock.unlock();
return Open(codecId, par, timebase, true, 0, 0);
}
LOGINFO("videocodec: %s: %s (%s) for codec \"%s\" opened%s, using %s decoding with %d threads%s",
m_identifier,
codec->long_name ? codec->long_name : codec->name,
codec->name,
avcodec_get_name(codecId),
forceSoftwareDecoder ? " (sw decoding forced)" : "",
m_isHardwareDecoder ? "hardware" : "software",
m_pVideoCtx->thread_count,
m_isHardwareDecoder ? " 🤩" : "");
m_pCodecString = codec->long_name ? codec->long_name : codec->name;
m_cntPacketsSent = m_cntFramesReceived = 0;
m_cntStartKeyFrames = 1;
return 0;
}
/**
* Close video decoder
*/
void cVideoDecoder::Close(void)
{
std::lock_guard<std::mutex> lock(m_mutex);
if (m_pVideoCtx != nullptr) {
LOGDEBUG2(L_CODEC, "videocodec: %s: %s: m_pVideoCtx %p", m_identifier, __FUNCTION__, m_pVideoCtx);
m_lastCodedWidth = m_pVideoCtx->coded_width;
m_lastCodedHeight = m_pVideoCtx->coded_height;
avcodec_free_context(&m_pVideoCtx);
m_pVideoCtx = nullptr;
}
m_cntPacketsSent = m_cntFramesReceived = 0;
}
/**
* Get extradata from avpkt
*
* @param avpkt video packet
*
* @retval 0 extradata set
* @retval -1 something went wrong
*/
int cVideoDecoder::GetExtraData(const AVPacket * avpkt)
{
AVBSFContext *bsfCtx;
const AVBitStreamFilter *f;
size_t extradataSize;
uint8_t *extradata;
int ret = 0;
f = av_bsf_get_by_name("extract_extradata");
if (!f) {
LOGERROR("videocodec: %s: %s: extradata av_bsf_get_by_name failed!", m_identifier, __FUNCTION__);
return -1;
}
ret = av_bsf_alloc(f, &bsfCtx);
if (ret < 0) {
LOGERROR("videocodec: %s: %s: extradata av_bsf_alloc failed!", m_identifier, __FUNCTION__);
return ret;
}
bsfCtx->par_in->codec_id = m_pVideoCtx->codec_id;
ret = av_bsf_init(bsfCtx);
if (ret < 0) {
LOGERROR("videocodec: %s: %s: extradata av_bsf_init failed!", m_identifier, __FUNCTION__);
av_bsf_free(&bsfCtx);
return ret;
}
AVPacket *dstPkt = av_packet_alloc();
AVPacket *pktRef = dstPkt;
if (!dstPkt) {
LOGERROR("videocodec: %s: %s: extradata av_packet_alloc failed!", m_identifier, __FUNCTION__);
av_bsf_free(&bsfCtx);
return -1;
}
ret = av_packet_ref(pktRef, avpkt);
if (ret < 0) {
LOGERROR("videocodec: %s: %s: extradata av_packet_ref failed!", m_identifier, __FUNCTION__);
av_packet_free(&dstPkt);
av_bsf_free(&bsfCtx);
return ret;
}
ret = av_bsf_send_packet(bsfCtx, pktRef);
if (ret < 0) {
LOGERROR("videocodec: %s: %s: extradata av_bsf_send_packet failed!", m_identifier, __FUNCTION__);
av_packet_unref(pktRef);
av_packet_free(&dstPkt);
av_bsf_free(&bsfCtx);
return ret;
}
ret = av_bsf_receive_packet(bsfCtx, pktRef);
if (ret < 0) {
LOGERROR("videocodec: %s: %s: extradata av_bsf_receive_packet failed!", m_identifier, __FUNCTION__);
av_packet_unref(pktRef);
av_packet_free(&dstPkt);
av_bsf_free(&bsfCtx);
return ret;
}
extradata = av_packet_get_side_data(pktRef, AV_PKT_DATA_NEW_EXTRADATA, &extradataSize);
m_pVideoCtx->extradata = (uint8_t *)av_mallocz(extradataSize + AV_INPUT_BUFFER_PADDING_SIZE);
memcpy(m_pVideoCtx->extradata, extradata, extradataSize);
m_pVideoCtx->extradata_size = extradataSize;
av_packet_unref(pktRef);
av_packet_free(&dstPkt);
av_bsf_free(&bsfCtx);
return ret;
}
/**
* Send a video packet to be decoded
*
* @param avpkt video packet
*
* @retval 0 packet was sent
* @retval AVERROR(EAGAIN) packet was not accepted, first receive frame and send packet again
* @retval AVERROR(EINVAL) invalid input or missing m_pVideoCtx
* @retval ret other ffmpeg error
*/
int cVideoDecoder::SendPacket(const AVPacket *avpkt)
{
std::lock_guard<std::mutex> lock(m_mutex);
int ret = 0;
if (m_pVideoCtx == nullptr)
return AVERROR(EINVAL);
// force a flush, if avpkt is NULL, this initiates a decoder drain
if (!avpkt) {
LOGDEBUG2(L_CODEC, "videocodec: %s: %s: send NULL packet, flush requested", m_identifier, __FUNCTION__);
avcodec_send_packet(m_pVideoCtx, NULL);
return 0;
}
if (!avpkt->size)
return AVERROR(EINVAL);
// get extradata, if not yet done
if (!m_pVideoCtx->extradata_size) {
if (!GetExtraData(avpkt))
LOGDEBUG2(L_CODEC, "videocodec: %s: %s: set extradata %p %d", m_identifier, __FUNCTION__, m_pVideoCtx->extradata, m_pVideoCtx->extradata_size);
}
ret = avcodec_send_packet(m_pVideoCtx, avpkt);
if (ret) {
if (ret != AVERROR(EAGAIN))
LOGDEBUG2(L_CODEC, "videocodec: %s: %s: send_packet ret: %s", m_identifier, __FUNCTION__, av_err2str(ret));
return ret;
}
m_cntPacketsSent++;
LOGDEBUG2(L_PACKET, "videocodec: %s: %s: %6d PTS %s <<---", m_identifier, __FUNCTION__, m_cntPacketsSent, Timestamp2String(avpkt->pts, 90));
return 0;
}
/**
* Receive a decoded a video frame
*
* @param[out] frame decoded AVFrame
* @param instance instance name for logging
*
* @retval 0 received frame
* @retval AVERROR(EAGAIN) get no frame, send avpkt again
* @retval AVERROR_EOF EOF, needs flushing
* @retval AVERROR(EINVAL) get no frame, something went wrong
* @retval ret return other ffmpeg error
*/
int cVideoDecoder::ReceiveFrame(AVFrame **frame)
{
std::lock_guard<std::mutex> lock(m_mutex);
int ret;
AVFrame *pFrame;
if (m_pVideoCtx == nullptr)
return AVERROR(EINVAL);
if (!(pFrame = av_frame_alloc()))
LOGFATAL("videocodec: %s: %s: can't allocate decoder frame", m_identifier, __FUNCTION__);
ret = avcodec_receive_frame(m_pVideoCtx, pFrame);
if (ret) {
if (ret == AVERROR_EOF)
LOGDEBUG2(L_CODEC, "videocodec: %s: %s: receive_frame ret: AVERROR_EOF", m_identifier, __FUNCTION__);
else if (ret != AVERROR(EAGAIN))
LOGDEBUG2(L_CODEC, "videocodec: %s: %s: receive_frame ret: %s", m_identifier, __FUNCTION__, av_err2str(ret));
av_frame_free(&pFrame);
return ret;
}
if (pFrame->flags == AV_FRAME_FLAG_CORRUPT)
LOGDEBUG2(L_CODEC, "videocodec: %s: %s: AV_FRAME_FLAG_CORRUPT", m_identifier, __FUNCTION__);
// Codec artifacts workaround for amlogic H264:
// Skip m_skipKeyFramesNum Key-Frames at stream start.
// m_skipKeyFramesNum can be set with SetSkipKeyFramesNum()
if (m_pVideoCtx->codec_id == AV_CODEC_ID_H264 && m_skipKeyFramesNum && m_cntStartKeyFrames) {
if (IsKeyFrame(pFrame)) {
LOGDEBUG2(L_CODEC, "videocodec: %s: %s: artifact workaround - skip %s Keyframe nr %d", m_identifier, __FUNCTION__,
isInterlacedFrame(pFrame) ? "interlaced" : "progressive", m_cntStartKeyFrames);
if (m_cntStartKeyFrames++ > m_skipKeyFramesNum - 1)
m_cntStartKeyFrames = 0;
}
av_frame_free(&pFrame);
return AVERROR(EAGAIN);
}
*frame = pFrame;
m_cntFramesReceived++;
LOGDEBUG2(L_PACKET, "videocodec: %s: %s: %6d PTS %s --->> (%2d)%s", m_identifier, __FUNCTION__,
m_cntFramesReceived, Timestamp2String(pFrame->pts, 90), m_cntPacketsSent - m_cntFramesReceived,
isInterlacedFrame(pFrame) ? " I" : "");
return 0;
}
/**
* Reopen the video decoder
*
* @param codecId video codec id
* @param par codec parameters
* @param timebase timebase
* @param forceSoftwareDecoding force software decoding
*
* @retval 0 success
* @retval -1 reopen decoder failed
*
* @todo
* This is just a temporary implementation
* RPi's ffmpeg decoder is broken. In order to get the same result if
* we want to flush the decoder, we need to close and reopen it.
* This function is only needed, if some decoder can't flush correctly.
* Once this is fixed in ffmpeg, we can drop this function.
* remove, once ffmpeg is fixed
*/
int cVideoDecoder::ReopenCodec(enum AVCodecID codecId, AVCodecParameters *par,
AVRational timebase, int forceSoftwareDecoding)
{
LOGDEBUG2(L_CODEC, "videocodec: %s: %s: m_pVideoCtx %p", m_identifier, __FUNCTION__, m_pVideoCtx);
Close();
if (Open(codecId, par, timebase, forceSoftwareDecoding, m_lastCodedWidth, m_lastCodedHeight))
return -1;
m_cntStartKeyFrames = 0; // currently unused, because we have no hardware which needs both quirks
m_cntPacketsSent = m_cntFramesReceived = 0;
return 0;
}
/**
* Flush the video decoder buffers
*
* Also reset packet sent/ frame received counter
*/
void cVideoDecoder::FlushBuffers(void)
{
std::lock_guard<std::mutex> lock(m_mutex);
LOGDEBUG2(L_CODEC, "videocodec: %s: %s: m_pVideoCtx %p", m_identifier, __FUNCTION__, m_pVideoCtx);
if (m_pVideoCtx)
avcodec_flush_buffers(m_pVideoCtx);
m_cntPacketsSent = m_cntFramesReceived = 0;
}
/**
* Check, if this is a key frame
*
* @param frame AVFrame
*
* @return true, if this frame is a key frame
*/
bool cVideoDecoder::IsKeyFrame(AVFrame *frame)
{
#if LIBAVUTIL_VERSION_INT < AV_VERSION_INT(58,7,100)
return frame->key_frame;
#else
return frame->flags & AV_FRAME_FLAG_KEY;
#endif
}