File: FFmpeg.cpp

package info (click to toggle)
kodi 2%3A20.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 143,820 kB
  • sloc: cpp: 664,925; xml: 68,398; ansic: 37,223; python: 6,903; sh: 4,209; javascript: 2,325; makefile: 1,754; perl: 969; java: 513; cs: 390; objc: 340
file content (285 lines) | stat: -rw-r--r-- 7,323 bytes parent folder | download
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
/*
 *  Copyright (C) 2005-2018 Team Kodi
 *  This file is part of Kodi - https://kodi.tv
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 *  See LICENSES/README.md for more information.
 */

#include "cores/FFmpeg.h"

#include "ServiceBroker.h"
#include "settings/AdvancedSettings.h"
#include "settings/SettingsComponent.h"
#include "threads/CriticalSection.h"
#include "threads/Thread.h"
#include "utils/StringUtils.h"
#include "utils/log.h"

extern "C"
{
#include <libavcodec/bsf.h>
}

#include <map>
#include <mutex>

static thread_local CFFmpegLog* CFFmpegLogTls;

namespace FFMPEG_HELP_TOOLS
{

std::string FFMpegErrorToString(int err)
{
  std::string text;
  text.resize(AV_ERROR_MAX_STRING_SIZE);
  av_strerror(err, text.data(), AV_ERROR_MAX_STRING_SIZE);
  return text;
}

} // namespace FFMPEG_HELP_TOOLS

void CFFmpegLog::SetLogLevel(int level)
{
  CFFmpegLog::ClearLogLevel();
  CFFmpegLog *log = new CFFmpegLog();
  log->level = level;
  CFFmpegLogTls = log;
}

int CFFmpegLog::GetLogLevel()
{
  CFFmpegLog* log = CFFmpegLogTls;
  if (!log)
    return -1;
  return log->level;
}

void CFFmpegLog::ClearLogLevel()
{
  CFFmpegLog* log = CFFmpegLogTls;
  CFFmpegLogTls = nullptr;
  if (log)
    delete log;
}

static CCriticalSection m_logSection;
std::map<const CThread*, std::string> g_logbuffer;

void ff_flush_avutil_log_buffers(void)
{
  std::unique_lock<CCriticalSection> lock(m_logSection);
  /* Loop through the logbuffer list and remove any blank buffers
     If the thread using the buffer is still active, it will just
     add a new buffer next time it writes to the log */
  std::map<const CThread*, std::string>::iterator it;
  for (it = g_logbuffer.begin(); it != g_logbuffer.end(); )
    if ((*it).second.empty())
      g_logbuffer.erase(it++);
    else
      ++it;
}

void ff_avutil_log(void* ptr, int level, const char* format, va_list va)
{
  std::unique_lock<CCriticalSection> lock(m_logSection);
  const CThread* threadId = CThread::GetCurrentThread();
  std::string &buffer = g_logbuffer[threadId];

  AVClass* avc= ptr ? *(AVClass**)ptr : NULL;

  int maxLevel = AV_LOG_WARNING;
  if (CFFmpegLog::GetLogLevel() > 0)
    maxLevel = AV_LOG_INFO;

  if (level > maxLevel && !CServiceBroker::GetLogging().CanLogComponent(LOGFFMPEG))
    return;
  else if (CServiceBroker::GetSettingsComponent()->GetAdvancedSettings()->m_logLevel <= LOG_LEVEL_NORMAL)
    return;

  int type;
  switch (level)
  {
    case AV_LOG_INFO:
      type = LOGINFO;
      break;

    case AV_LOG_ERROR:
      type = LOGERROR;
      break;

    case AV_LOG_DEBUG:
    default:
      type = LOGDEBUG;
      break;
  }

  std::string message = StringUtils::FormatV(format, va);
  std::string prefix = StringUtils::Format("ffmpeg[{}]: ", fmt::ptr(threadId));
  if (avc)
  {
    if (avc->item_name)
      prefix += std::string("[") + avc->item_name(ptr) + "] ";
    else if (avc->class_name)
      prefix += std::string("[") + avc->class_name + "] ";
  }

  buffer += message;
  int pos, start = 0;
  while ((pos = buffer.find_first_of('\n', start)) >= 0)
  {
    if (pos > start)
      CLog::Log(type, "{}{}", prefix, buffer.substr(start, pos - start));
    start = pos+1;
  }
  buffer.erase(0, start);
}

std::tuple<uint8_t*, int> GetPacketExtradata(const AVPacket* pkt,
                                             const AVCodecParserContext* parserCtx,
                                             AVCodecContext* codecCtx)
{
  constexpr int FF_MAX_EXTRADATA_SIZE = ((1 << 28) - AV_INPUT_BUFFER_PADDING_SIZE);

  if (!pkt)
    return std::make_tuple(nullptr, 0);

  uint8_t* extraData = nullptr;
  int extraDataSize = 0;

#if LIBAVFORMAT_BUILD >= AV_VERSION_INT(59, 0, 100)
  /* extract_extradata bitstream filter is implemented only
   * for certain codecs, as noted in discussion of PR#21248
   */

  AVCodecID codecId = codecCtx->codec_id;

  // clang-format off
  if (
    codecId != AV_CODEC_ID_MPEG1VIDEO &&
    codecId != AV_CODEC_ID_MPEG2VIDEO &&
    codecId != AV_CODEC_ID_H264 &&
    codecId != AV_CODEC_ID_HEVC &&
    codecId != AV_CODEC_ID_MPEG4 &&
    codecId != AV_CODEC_ID_VC1 &&
    codecId != AV_CODEC_ID_AV1 &&
    codecId != AV_CODEC_ID_AVS2 &&
    codecId != AV_CODEC_ID_AVS3 &&
    codecId != AV_CODEC_ID_CAVS
  )
    // clang-format on
    return std::make_tuple(nullptr, 0);

  const AVBitStreamFilter* f = av_bsf_get_by_name("extract_extradata");
  if (!f)
    return std::make_tuple(nullptr, 0);

  AVBSFContext* bsf = nullptr;
  int ret = av_bsf_alloc(f, &bsf);
  if (ret < 0)
    return std::make_tuple(nullptr, 0);

  bsf->par_in->codec_id = codecId;

  ret = av_bsf_init(bsf);
  if (ret < 0)
  {
    av_bsf_free(&bsf);
    return std::make_tuple(nullptr, 0);
  }

  AVPacket* dstPkt = av_packet_alloc();
  if (!dstPkt)
  {
    CLog::LogF(LOGERROR, "failed to allocate packet");

    av_bsf_free(&bsf);
    return std::make_tuple(nullptr, 0);
  }
  AVPacket* pktRef = dstPkt;

  ret = av_packet_ref(pktRef, pkt);
  if (ret < 0)
  {
    av_bsf_free(&bsf);
    av_packet_free(&dstPkt);
    return std::make_tuple(nullptr, 0);
  }

  ret = av_bsf_send_packet(bsf, pktRef);
  if (ret < 0)
  {
    av_packet_unref(pktRef);
    av_bsf_free(&bsf);
    av_packet_free(&dstPkt);
    return std::make_tuple(nullptr, 0);
  }

  ret = 0;
  while (ret >= 0)
  {
    ret = av_bsf_receive_packet(bsf, pktRef);
    if (ret < 0)
    {
      if (ret != AVERROR(EAGAIN) && ret != AVERROR_EOF)
        break;

      continue;
    }

    size_t retExtraDataSize = 0;
    uint8_t* retExtraData =
        av_packet_get_side_data(pktRef, AV_PKT_DATA_NEW_EXTRADATA, &retExtraDataSize);
    if (retExtraData && retExtraDataSize > 0 && retExtraDataSize < FF_MAX_EXTRADATA_SIZE)
    {
      extraData = static_cast<uint8_t*>(av_malloc(retExtraDataSize + AV_INPUT_BUFFER_PADDING_SIZE));
      if (!extraData)
      {
        CLog::LogF(LOGERROR, "failed to allocate {} bytes for extradata", retExtraDataSize);

        av_packet_unref(pktRef);
        av_bsf_free(&bsf);
        av_packet_free(&dstPkt);
        return std::make_tuple(nullptr, 0);
      }

      CLog::LogF(LOGDEBUG, "fetching extradata, extradata_size({})", retExtraDataSize);

      memcpy(extraData, retExtraData, retExtraDataSize);
      memset(extraData + retExtraDataSize, 0, AV_INPUT_BUFFER_PADDING_SIZE);
      extraDataSize = retExtraDataSize;

      av_packet_unref(pktRef);
      break;
    }

    av_packet_unref(pktRef);
  }

  av_bsf_free(&bsf);
  av_packet_free(&dstPkt);
#else
  if (codecCtx && parserCtx && parserCtx->parser && parserCtx->parser->split)
    extraDataSize = parserCtx->parser->split(codecCtx, pkt->data, pkt->size);

  if (extraDataSize <= 0 || extraDataSize >= FF_MAX_EXTRADATA_SIZE)
  {
    CLog::LogF(LOGDEBUG, "fetched extradata of weird size {}", extraDataSize);
    return std::make_tuple(nullptr, 0);
  }

  extraData = static_cast<uint8_t*>(av_malloc(extraDataSize + AV_INPUT_BUFFER_PADDING_SIZE));
  if (!extraData)
  {
    CLog::LogF(LOGERROR, "failed to allocate {} bytes for extradata", extraDataSize);
    return std::make_tuple(nullptr, 0);
  }

  CLog::LogF(LOGDEBUG, "fetching extradata, extradata_size({})", extraDataSize);

  memcpy(extraData, pkt->data, extraDataSize);
  memset(extraData + extraDataSize, 0, AV_INPUT_BUFFER_PADDING_SIZE);
#endif

  return std::make_tuple(extraData, extraDataSize);
}