File: OggEncoder.cpp

package info (click to toggle)
kwave 25.04.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,272 kB
  • sloc: cpp: 56,173; xml: 817; perl: 688; sh: 57; makefile: 11
file content (142 lines) | stat: -rw-r--r-- 4,735 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
/*************************************************************************
         OggEncoder.cpp  -  encoder for Ogg/Vorbis data
                             -------------------
    begin                : Tue Sep 10 2002
    copyright            : (C) 2002 by Thomas Eschenbacher
    email                : Thomas.Eschenbacher@gmx.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "config.h"

#include <math.h>
#include <stdlib.h>
#include <new>

#include <QIODevice>
#include <QList>
#include <QSharedPointer>

#include <KLocalizedString>

#include "libkwave/FileInfo.h"
#include "libkwave/MessageBox.h"
#include "libkwave/MetaDataList.h"
#include "libkwave/MultiTrackReader.h"
#include "libkwave/Sample.h"

#include "OggCodecPlugin.h"
#include "OggEncoder.h"
#include "OggSubEncoder.h"
#include "OpusEncoder.h"
#include "VorbisEncoder.h"


/***************************************************************************/
Kwave::OggEncoder::OggEncoder()
    :Kwave::Encoder(), m_comments_map()
{
#ifdef HAVE_OGG_OPUS
    REGISTER_OGG_OPUS_MIME_TYPES
    REGISTER_COMPRESSION_TYPE_OGG_OPUS
#endif /* HAVE_OGG_OPUS */

#ifdef HAVE_OGG_VORBIS
    REGISTER_OGG_VORBIS_MIME_TYPES
    REGISTER_COMPRESSION_TYPE_OGG_VORBIS
#endif /* HAVE_OGG_VORBIS */
}

/***************************************************************************/
Kwave::OggEncoder::~OggEncoder()
{
}

/***************************************************************************/
Kwave::Encoder *Kwave::OggEncoder::instance()
{
    return new(std::nothrow) Kwave::OggEncoder();
}

/***************************************************************************/
QList<Kwave::FileProperty> Kwave::OggEncoder::supportedProperties()
{
    return m_comments_map.values();
}

/***************************************************************************/
bool Kwave::OggEncoder::encode(QWidget *widget, Kwave::MultiTrackReader &src,
                               QIODevice &dst,
                               const Kwave::MetaDataList &meta_data)
{
    const Kwave::FileInfo info(meta_data);
    QSharedPointer<Kwave::OggSubEncoder> sub_encoder;

    // determine which codec (sub encoder) to use,
    // by examining the compression type
    const Kwave::Compression::Type compression =
        info.contains(Kwave::INF_COMPRESSION) ? Kwave::Compression::fromInt(
        info.get(Kwave::INF_COMPRESSION).toInt()) : Kwave::Compression::NONE;

#ifdef HAVE_OGG_OPUS
    if (compression == Compression::OGG_OPUS) {
        qDebug("    OggEncoder: using Opus codec");
        sub_encoder = QSharedPointer<Kwave::OpusEncoder>(
            new(std::nothrow) Kwave::OpusEncoder()
        );
    }
#endif /* HAVE_OGG_OPUS */
#ifdef HAVE_OGG_VORBIS
    if (compression == Compression::OGG_VORBIS) {
        qDebug("    OggEncoder: using Vorbis codec");
        sub_encoder = QSharedPointer<Kwave::VorbisEncoder>(
            new(std::nothrow) Kwave::VorbisEncoder()
        );
    }
#endif /* HAVE_OGG_VORBIS */

    if (sub_encoder.isNull()) {
        qDebug("    OggEncoder: compression='%d'", compression);
        Kwave::MessageBox::error(widget, i18nc(
            "error in Ogg encoder, no support for a compression type "
            "(e.g. opus, vorbis etc)",
            "Error: No Codec for '%1' available",
            Kwave::Compression(compression).name()
        ));
        return false;
    }

    if (!sub_encoder->open(widget, info, src))
        return false;

    // open the output device
    if (!dst.open(QIODevice::ReadWrite | QIODevice::Truncate)) {
        Kwave::MessageBox::error(widget,
            i18n("Unable to open the file for saving."));
        return false;
    }

    if (!sub_encoder->writeHeader(dst))
        return false;

    if (!sub_encoder->encode(src, dst))
        return false;

    // clean up and exit.
    sub_encoder->close();

    // the sub encoder will be deleted automatically (QSharedPointer)

    return true;
}

//***************************************************************************
//***************************************************************************