summaryrefslogtreecommitdiffstats
path: root/plugins/decoder/ffmpeg/k3bffmpegwrapper.cpp
blob: 3e34c5c5f23f941d97ef9e7c527b93e044dc2afa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
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
/*
 *
 *
 * Copyright (C) 2004-2008 Sebastian Trueg <trueg@k3b.org>
 *
 * This file is part of the K3b project.
 * Copyright (C) 1998-2008 Sebastian Trueg <trueg@k3b.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * See the file "COPYING" for the exact licensing terms.
 */

#include <config.h>

#include "k3bffmpegwrapper.h"
#include <tdelocale.h>

extern "C" {
/*
 Recent versions of FFmpeg uses C99 constant macros which are not present in C++
 standard. The macro __STDC_CONSTANT_MACROS allow C++ to use these macros.
 Although it's not defined by C++ standard it's supported by many
 implementations. See bug 236036 and discussion:
 https://lists.ffmpeg.org/pipermail/ffmpeg-devel/2010-May/095488.html
 */
#define __STDC_CONSTANT_MACROS
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
}

#include <math.h>
#include <string.h>

#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(52, 101, 0)
#define av_dump_format(c, x, f, y) dump_format(c, x, f, y)
#endif
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(53, 2, 0)
//      this works because the parameters/options are not used
#define avformat_open_input(c, s, f, o) av_open_input_file(c, s, f, 0, o)
#endif
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(53, 6, 0)
#define avformat_find_stream_info(c, o) av_find_stream_info(c)
#endif
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(53, 17, 0)
#define avformat_close_input(c) av_close_input_file(*c)
#endif
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(57, 41, 100)
#define codecpar codec
#endif

#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55, 45, 101)
#define av_frame_alloc avcodec_alloc_frame
#endif
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54, 28, 0)
#define av_frame_free(f) av_free(*(f))
#elif LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55, 45, 101)
#define av_frame_free  avcodec_free_frame
#endif

#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(52, 64, 0)
#define AVMEDIA_TYPE_AUDIO CODEC_TYPE_AUDIO
#define AVMEDIA_TYPE_VIDEO CODEC_TYPE_VIDEO
#define AVMEDIA_TYPE_SUBTITLE CODEC_TYPE_SUBTITLE
#endif
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(53, 8, 0)
#define avcodec_open2(a, c, o) avcodec_open(a, c)
#endif
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(54, 25, 0)
// From libavcodec version 54.25, CodecID have been renamed to AVCodecID and all
// CODEC_ID_* to AV_CODEC_ID_*. This code can be simplified once all supported
// distros have updated to libavcodec version >=54.25
#define AV_CODEC_ID_WMAV1 CODEC_ID_WMAV1
#define AV_CODEC_ID_WMAV2 CODEC_ID_WMAV2
#define AV_CODEC_ID_MP3 CODEC_ID_MP3
#define AV_CODEC_ID_AAC CODEC_ID_AAC
#define AV_CODEC_ID_APE CODEC_ID_APE
#define AV_CODEC_ID_WAVPACK CODEC_ID_WAVPACK
#endif
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55,39,101)
#define av_packet_unref av_free_packet
#endif

// TODO:  most of the used av_functions there are deprecated and there
//        are troubles with improper frame/packet processing that
//        leads to aborting of decode process
//
//        [wmav2 @ 0xxxxx] Multiple frames in a packet.
//        [wmav2 @ 0xxxxx] Got unexpected packet size after a partial decode

K3bFFMpegWrapper *K3bFFMpegWrapper::s_instance = NULL;

class K3bFFMpegFile::Private {
public:
  TQ_UINT8 *packetData;
  K3b::Msf length;

  ::AVFormatContext *formatContext;
  ::AVCodec *codec;
  ::AVStream *audio_stream;
  ::AVCodecContext *audio_stream_ctx;
  ::AVSampleFormat sampleFormat;
  ::AVFrame *frame;
  ::AVPacket *packet;
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 12, 100)
  ::AVPacket _packet;
#endif

  char *outputBufferPos;
  int outputBufferSize;
  int packetSize;
  bool isSpacious;
};

K3bFFMpegFile::K3bFFMpegFile(const TQString &filename) : m_filename(filename) {
  d = new Private;
  d->formatContext = NULL;
  d->codec = NULL;
  d->audio_stream = NULL;
  d->audio_stream_ctx = NULL;
  d->frame = av_frame_alloc();
  d->outputBufferPos = NULL;
  d->packet = NULL;
}

K3bFFMpegFile::~K3bFFMpegFile() {
  close();
  av_frame_free(&d->frame);
  delete d;
}

bool K3bFFMpegFile::open() {
  close();

  // open the file
  int err = ::avformat_open_input(&d->formatContext, m_filename.local8Bit(),
                                  NULL, NULL);
  if (err < 0) {
    kdDebug() << "(K3bFFMpegFile) unable to open " << m_filename
              << " with error " << err;
    return false;
  }

  // analyze the streams
  ::avformat_find_stream_info(d->formatContext, NULL);

  // we only handle files containing one audio stream
  for (uint i = 0; i < d->formatContext->nb_streams; ++i) {
    if (d->formatContext->streams[i]->codecpar->codec_type ==
        AVMEDIA_TYPE_AUDIO) {
      if (!d->audio_stream) {
        d->audio_stream = d->formatContext->streams[i];
      } else {
        d->audio_stream = NULL;
        kdDebug() << "(K3bFFMpegFile) more than one audio stream in "
                  << m_filename;
        return false;
      }
    }
  }

  // urgh... ugly
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 33, 100)
  if (d->audio_stream->codecpar->codec_type != AVMEDIA_TYPE_AUDIO)
#else
  d->audio_stream_ctx = d->audio_stream->codec;
  if (d->audio_stream_ctx->codec_type != AVMEDIA_TYPE_AUDIO)
#endif
  {
    kdDebug() << "(K3bFFMpegFile) not a simple audio stream: " << m_filename;
    return false;
  }

  // get the codec
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 33, 100)
  d->codec = (AVCodec *)::avcodec_find_decoder(d->audio_stream->codecpar->codec_id);
#else
  d->codec = (AVCodec *)::avcodec_find_decoder(d->audio_stream_ctx->codec_id);
#endif
  if (!d->codec) {
    kdDebug() << "(K3bFFMpegFile) no codec found for " << m_filename;
    return false;
  }

#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 33, 100)
  // allocate a codec context
  d->audio_stream_ctx = avcodec_alloc_context3(d->codec);
  if (d->audio_stream_ctx) {
    avcodec_parameters_to_context(d->audio_stream_ctx, d->audio_stream->codecpar);
  }
  else {
    kdDebug() << "(K3bFFMpegFile) failed to allocate a codec context for "
              << m_filename;
  }
#endif

  // open the codec on our context
  kdDebug() << "(K3bFFMpegFile) found codec for " << m_filename << endl;
  if (::avcodec_open2(d->audio_stream_ctx, d->codec, NULL) < 0) {
    kdDebug() << "(K3bFFMpegDecoderFactory) could not open codec.";
    return false;
  }

  // determine the length of the stream
  d->length = K3b::Msf::fromSeconds(double(d->formatContext->duration) /
                                    double(AV_TIME_BASE));

  if (d->length == 0) {
    kdDebug() << "(K3bFFMpegDecoderFactory) invalid length.";
    return false;
  }

#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(57, 41, 100)
  d->sampleFormat = d->audio_stream->codec->sample_fmt;
#else
  d->sampleFormat = static_cast<::AVSampleFormat>(d->audio_stream->codecpar->format);
#endif
# if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59, 24, 100)
  d->isSpacious = ::av_sample_fmt_is_planar(d->sampleFormat) &&
                  d->audio_stream->codecpar->channels > 1;
# else
  d->isSpacious = ::av_sample_fmt_is_planar(d->sampleFormat) &&
                  d->audio_stream->codecpar->ch_layout.nb_channels > 1;
# endif

  // dump some debugging info
  ::av_dump_format(d->formatContext, 0, m_filename.local8Bit(), 0);

  return true;
}

void K3bFFMpegFile::close() {
  d->outputBufferSize = 0;
  d->packetSize = 0;
  d->packetData = NULL;

  if (d->codec) {
#if LIBAVFORMAT_VERSION_INT >= AV_VERSION_INT(57, 33, 100)
    ::avcodec_free_context(&d->audio_stream_ctx);
#else
    ::avcodec_close(d->audio_stream_ctx);
    d->codec = NULL;
#endif
  }

  if (d->formatContext) {
    ::avformat_close_input(&d->formatContext);
    d->formatContext = NULL;
  }

  d->audio_stream = NULL;
}

K3b::Msf K3bFFMpegFile::length() const { return d->length; }

int K3bFFMpegFile::sampleRate() const {
  return d->audio_stream->codecpar->sample_rate;
}

int K3bFFMpegFile::channels() const {
# if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(59, 24, 100)
  return d->audio_stream->codecpar->channels;
# else
  return d->audio_stream->codecpar->ch_layout.nb_channels;
# endif
}

int K3bFFMpegFile::type() const { return d->audio_stream->codecpar->codec_id; }

TQString K3bFFMpegFile::typeComment() const {
  switch (type()) {
  case AV_CODEC_ID_WMAV1:
    return i18n("Windows Media v1");
  case AV_CODEC_ID_WMAV2:
    return i18n("Windows Media v2");
  case AV_CODEC_ID_WAVPACK:
    return i18n("WavPack");
  case AV_CODEC_ID_APE:
    return i18n("Monkey's Audio (APE)");
  case AV_CODEC_ID_AAC:
    return i18n("Advanced Audio Coding (AAC)");
  default:
    return TQString::fromLocal8Bit(d->codec->name);
  }
}

TQString K3bFFMpegFile::title() const {
  // FIXME: is this UTF8 or something??
  AVDictionaryEntry *ade =
      av_dict_get(d->formatContext->metadata, "TITLE", NULL, 0);
  return ade && ade->value && ade->value[0] != '\0'
             ? TQString::fromLocal8Bit(ade->value)
             : TQString();
}

TQString K3bFFMpegFile::author() const {
  // FIXME: is this UTF8 or something??
  AVDictionaryEntry *ade =
      av_dict_get(d->formatContext->metadata, "ARTIST", NULL, 0);
  return ade && ade->value && ade->value[0] != '\0'
             ? TQString::fromLocal8Bit(ade->value)
             : TQString();
}

TQString K3bFFMpegFile::comment() const {
  // FIXME: is this UTF8 or something??
  AVDictionaryEntry *ade =
      av_dict_get(d->formatContext->metadata, "COMMENT", NULL, 0);
  return ade && ade->value && ade->value[0] != '\0'
             ? TQString::fromLocal8Bit(ade->value)
             : TQString();
}

int K3bFFMpegFile::read(char *buf, int bufLen) {

  int ret = fillOutputBuffer();
  if (ret <= 0) {
    return ret;
  }

  int len = TQMIN(bufLen, d->outputBufferSize);
  ::memcpy(buf, d->outputBufferPos, len);

  if (d->isSpacious && bufLen > d->outputBufferSize)
    delete[] d->outputBufferPos; // clean up allocated space

  // TODO: only swap if needed
  for (int i = 0; i < len - 1; i += 2)
    tqSwap(buf[i], buf[i + 1]); // BE -> LE

  d->outputBufferSize -= len;
  if (d->outputBufferSize > 0)
    d->outputBufferPos += len;
  return len;
}

// fill d->packetData with data to decode
int K3bFFMpegFile::readPacket() {
  if (d->packetSize <= 0) {
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 12, 100)
    d->packet = ::av_packet_alloc();
#else
    ::av_init_packet(&d->_packet);
    d->packet = &d->_packet;
#endif

    if (::av_read_frame(d->formatContext, d->packet) < 0) {
      return 0;
    }
    d->packetSize = d->packet->size;
    d->packetData = d->packet->data;
  }

  return d->packetSize;
}

// decode data in d->packetData and fill d->outputBuffer
int K3bFFMpegFile::fillOutputBuffer() {
  // decode if the output buffer is empty
  while (d->outputBufferSize <= 0) {

    // make sure we have data to decode
    if (readPacket() == 0) {
      return 0;
    }

    int gotFrame = 0;
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 106, 100)
    int len = avcodec_receive_frame(d->audio_stream_ctx, d->frame);
    if (len == 0) {
      gotFrame = 1;
    }
    else if (len == AVERROR(EAGAIN)) {
      len = 0;
    }

    if (len == 0) {
      len = avcodec_send_packet(d->audio_stream_ctx, d->packet);
      if (len == AVERROR(EAGAIN)) {
        len = 0;
      }
    }
#else
    int len = ::avcodec_decode_audio4(d->audio_stream_ctx, d->frame,
                                      &gotFrame, d->packet);
#endif

    if (d->packetSize <= 0 || len < 0) {
#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 12, 100)
      ::av_packet_free(&d->packet);
#else
      ::av_packet_unref(d->packet);
      d->packet = NULL;
#endif
    }
    if (len < 0) {
      kdDebug() << "(K3bFFMpegFile) decoding failed for " << m_filename;
      return -1;
    }

#if LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 12, 100)
    len = d->packet->size;
#endif

    if (gotFrame) {
      int nb_s = d->frame->nb_samples;
      int nb_ch = 2; // copy only two channels even if there're more
      d->outputBufferSize = nb_s * nb_ch * 2; // 2 means 2 bytes (16bit)
      d->outputBufferPos = reinterpret_cast<char *>(d->frame->extended_data[0]);
      if (d->isSpacious) {
        d->outputBufferPos = new char[d->outputBufferSize];
        if (d->sampleFormat == AV_SAMPLE_FMT_FLTP) {
          int width = sizeof(float); // sample width of float audio
          for (int sample = 0; sample < nb_s; sample++) {
            for (int ch = 0; ch < nb_ch; ch++) {
              double val = *(reinterpret_cast<float *>(
                  d->frame->extended_data[ch] + sample * width));
              val = ::abs(val) > 1 ? ::copysign(1.0, val) : val;
              int16_t result =
                  static_cast<int16_t>(val * 32767.0 + 32768.5) - 32768;
              ::memcpy(d->outputBufferPos + (sample * nb_ch + ch) * 2, &result,
                       2); // 2 is sample width of 16 bit audio
            }
          }
        } else {
          for (int sample = 0; sample < nb_s; sample++) {
            for (int ch = 0; ch < nb_ch; ch++) {
              ::memcpy(d->outputBufferPos + (sample * nb_ch + ch) * 2,
                       d->frame->extended_data[ch] + sample * 2,
                       2); // 16 bit here as well
            }
          }
        }
      }
    }
    d->packetSize -= len;
    d->packetData += len;
  }

  return d->outputBufferSize;
}

bool K3bFFMpegFile::seek(const K3b::Msf &msf) {
  d->outputBufferSize = 0;
  d->packetSize = 0;

  double seconds = double(msf.totalFrames()) / 75.0;
  int64_t timestamp = static_cast<int64_t>(seconds * double(AV_TIME_BASE));

  // FIXME: do we really need the start_time and why?
  return (::av_seek_frame(d->formatContext, -1,
                          timestamp + d->formatContext->start_time, 0) >= 0);
}

//
// av_register_all is deprecated since ffmpeg 4.0, can be dropped
K3bFFMpegWrapper::K3bFFMpegWrapper() {
#if LIBAVFORMAT_VERSION_INT < AV_VERSION_INT(58,9,100)
  ::av_register_all();
#endif
}

K3bFFMpegWrapper::~K3bFFMpegWrapper() { s_instance = NULL; }

K3bFFMpegWrapper *K3bFFMpegWrapper::instance() {
  if (!s_instance) {
    s_instance = new K3bFFMpegWrapper();
  }

  return s_instance;
}

K3bFFMpegFile *K3bFFMpegWrapper::open(const TQString &filename) const {
  K3bFFMpegFile *file = new K3bFFMpegFile(filename);
  if (file->open()) {
#ifndef K3B_FFMPEG_ALL_CODECS
    //
    // only allow tested formats. ffmpeg seems not to be too reliable with every
    // format. mp3 being one of them sadly. Most importantly: allow the
    // libsndfile decoder to do its thing.
    //
    if (file->type() == AV_CODEC_ID_WMAV1 ||
        file->type() == AV_CODEC_ID_WMAV2 || file->type() == AV_CODEC_ID_AAC ||
        file->type() == AV_CODEC_ID_APE || file->type() == AV_CODEC_ID_WAVPACK)
#endif
      return file;
  }

  delete file;
  return NULL;
}