summaryrefslogtreecommitdiff
path: root/convert.c
blob: 9eb70031f8eef5e01251bc0bf1311739864c9086 (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
/*
 * convert.c
 */

#include "convert.h"

#include <vdr/tools.h>

#ifndef AVCODEC_MAX_AUDIO_FRAME_SIZE
#define AVCODEC_MAX_AUDIO_FRAME_SIZE 192000 // 1 second of 48khz 32bit audio
#endif

/* --- cConvert ------------------------------------------------------------- */

cConvert::cConvert(const cPostData &postdata)
{
        encoder_buf.data = NULL;
        encoder_buf.length = 0;
        encoder_open = -1;

        decoder_buf.data = NULL;
        decoder_buf.length = 0;
        decoder_open = -1;

        init_decoder();
        init_encoder(audio_codecs[postdata.get_codec()], postdata.get_bit_rate(),
                postdata.get_sample_rate(), postdata.get_channels());
}


cConvert::~cConvert()
{
        delete[] encoder_buf.data;
        delete[] decoder_buf.data;

        if (decoder_open > -1) {
                avcodec_close(decoder_ctx);
                av_free(decoder_ctx);
        }

        if (encoder_open > -1) {
                avcodec_close(encoder_ctx);
                av_free(encoder_ctx);
        }
}


void  cConvert::init_decoder(void)
{
        decoder_codec = avcodec_find_decoder_by_name("mp2");
        if (! decoder_codec) {
                dsyslog("[audiorecorder]: codec mp2 is not supported (%s, "
                        "%s())", __FILE__,  __func__);
                return;
        }

        decoder_ctx = avcodec_alloc_context3(NULL);
        decoder_open = avcodec_open2(decoder_ctx, decoder_codec, NULL);

        if (decoder_open < 0) {
                dsyslog("[audiorecorder]: could not open codec mp2 (%s, "
                        "%s())", __FILE__, __func__);
                return;
        }

        decoder_buf.data = new uchar[AVCODEC_MAX_AUDIO_FRAME_SIZE];

        dsyslog("[audiorecorder]: decoder initialized (%s, %s())", __FILE__,
                __func__);
}


void cConvert::init_encoder(const char *codec, int bit_rate, int sample_rate,
        int channels)
{
        encoder_codec = avcodec_find_encoder_by_name(codec);
        if (! encoder_codec) {
                dsyslog("[audiorecorder]: codec %s is not supported (%s, "
                        "%s())", codec, __FILE__,  __func__);
                return;
        }

        encoder_ctx = avcodec_alloc_context3(NULL);

        encoder_ctx->bit_rate       = bit_rate;
        encoder_ctx->sample_fmt     = AV_SAMPLE_FMT_S16P;
        encoder_ctx->sample_rate    = sample_rate;
        encoder_ctx->channel_layout = AV_CH_LAYOUT_STEREO;
        encoder_ctx->channels       = channels;

        encoder_open = avcodec_open2(encoder_ctx, encoder_codec, NULL);

        if (encoder_open < 0) {
                dsyslog("[audiorecorder]: could not open codec %s (%s, %s())", codec, __FILE__, __func__);
                return;
        }

        encoder_buf.length = encoder_ctx->frame_size;
        encoder_buf.data = new uchar[encoder_buf.length];

        dsyslog("[audiorecorder]: encoder for %s-codec (br: %d, sr: %d, %d ch) "
                "initialized (%s, %s())", encoder_codec->name,
                encoder_ctx->bit_rate, encoder_ctx->sample_rate,
                encoder_ctx->channels, __FILE__, __func__);
}


void cConvert::decode_mpa_frame(mpeg_audio_frame *mpa_frame)
{
        if (decoder_open < 0) {
                decoder_buf.length = 0;
                return;
        }

//#ifndef AVCODEC_NEW
        AVPacket avpkt;
        av_init_packet(&avpkt);
        avpkt.data = mpa_frame->data;
        avpkt.size = mpa_frame->length;
        decoder_buf.length = AVCODEC_MAX_AUDIO_FRAME_SIZE;
        int len = avcodec_decode_audio3(decoder_ctx, (short *)decoder_buf.data,
                &decoder_buf.length, &avpkt);
//#else
//#error "avcodec_decode_audio4 not implemented yet!"
// ToDo

//#endif
}


abuffer *cConvert::reencode_mpa_frame(mpeg_audio_frame *mpa_frame,
        float volume)
{
        int c, len;
        short *tmp;

        if (decoder_open < 0 || encoder_open < 0)
                return &encoder_buf;

        if (strcmp(encoder_codec->name, "mp2") == 0 && volume == 1) {
                mpa_frame_buf.data = mpa_frame->data;
                mpa_frame_buf.offset = mpa_frame->length;
                return &mpa_frame_buf;
        }

        decode_mpa_frame(mpa_frame);

        if (volume != 1) {
                /* manipulate the volume */
                len = decoder_buf.length / sizeof(short);
                tmp = (short *)decoder_buf.data;

                for (c = 0; c < len; ++c) {
                        *(tmp) = (short)((float)*(tmp) * volume);
                        ++tmp;
                }
        }

#ifndef AVCODEC_NEW
        encoder_buf.offset = avcodec_encode_audio(encoder_ctx, encoder_buf.data,
                encoder_buf.length, (short *)decoder_buf.data);
        /* encoder_buf.offset is used to save the size of the encoded frame */
#else
#warning "you have enabled -DAVCODEC_NEW in Makefile"
#warning "this is still under development,"
#warning "and will compile not working code..."
// https://www.ffmpeg.org/doxygen/1.0/group__lavc__encoding.html#gf12a9da0d33f50ff406e03572fab4763
// https://www.ffmpeg.org/doxygen/1.0/decoding__encoding_8c-source.html#l00102
/* int avcodec_encode_audio2
		(
		AVCodecContext *  	avctx,
		AVPacket *  	avpkt,
		const AVFrame *  	frame,
		int *  	got_packet_ptr
		) */
        AVCodecContext *codec_ctx;
        AVFrame *frame;
        AVPacket avpkt;
        av_init_packet(&avpkt);
        avpkt.data = mpa_frame->data;
        avpkt.size = mpa_frame->length;
        int ret, got_output;

        /* frame containing input raw audio */
        frame = avcodec_alloc_frame();
        if (!frame) {
            dsyslog("[audiorecorder]: Could not allocate audio frame (%s, " "%s())",  __FILE__,  __func__);
            return;
//            fprintf(stderr, "Could not allocate audio frame\n");
//            exit(1);
         }

        encoder_buf.offset = avcodec_encode_audio2(encoder_ctx, &avpkt, frame, &got_output);
        if (ret < 0) {
            dsyslog("[audiorecorder]: Error encoding audio frame (%s, " "%s())",  __FILE__,  __func__);
            return;
//            fprintf(stderr, "Error encoding audio frame\n");
//            exit(1);
        }
#endif
        return &encoder_buf;
}