Skip to content

Commit 245901a

Browse files
committed
audio filter: support additional codecs
support additional input codecs in audio filters, output is currently hardcoded to AAC
1 parent 26f0687 commit 245901a

6 files changed

+64
-55
lines changed

vod/filters/audio_decoder.c

+36-17
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,62 @@
11
#include "audio_decoder.h"
22

33
// globals
4-
static const AVCodec *decoder_codec = NULL;
5-
static bool_t initialized = FALSE;
4+
static const AVCodec *decoder_codecs[VOD_CODEC_ID_COUNT];
5+
6+
typedef struct {
7+
const char *name;
8+
uint32_t vod_codec;
9+
enum AVCodecID av_codec;
10+
} audio_codec_mapping_t;
11+
12+
static audio_codec_mapping_t audio_codec_mapping[] = {
13+
{ "aac", VOD_CODEC_ID_AAC, AV_CODEC_ID_AAC },
14+
{ "ac3", VOD_CODEC_ID_AC3, AV_CODEC_ID_AC3 },
15+
{ "eac3", VOD_CODEC_ID_EAC3, AV_CODEC_ID_EAC3 },
16+
{ "mp3", VOD_CODEC_ID_MP3, AV_CODEC_ID_MP3 },
17+
{ "dts", VOD_CODEC_ID_DTS, AV_CODEC_ID_DTS },
18+
{ "vorbis", VOD_CODEC_ID_VORBIS, AV_CODEC_ID_VORBIS },
19+
{ "opus", VOD_CODEC_ID_OPUS, AV_CODEC_ID_OPUS },
20+
21+
{ NULL, VOD_CODEC_ID_INVALID, AV_CODEC_ID_NONE }
22+
};
623

724
void
825
audio_decoder_process_init(vod_log_t* log)
926
{
27+
audio_codec_mapping_t* mapping;
28+
const AVCodec* decoder_codec;
29+
1030
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 18, 100)
1131
avcodec_register_all();
1232
#endif
1333

14-
decoder_codec = avcodec_find_decoder(AV_CODEC_ID_AAC);
15-
if (decoder_codec == NULL)
34+
vod_memzero(decoder_codecs, sizeof(decoder_codecs));
35+
36+
for (mapping = audio_codec_mapping; mapping->vod_codec != VOD_CODEC_ID_INVALID; mapping++)
1637
{
17-
vod_log_error(VOD_LOG_WARN, log, 0,
18-
"audio_decoder_process_init: failed to get AAC decoder, audio decoding is disabled");
19-
return;
20-
}
38+
decoder_codec = avcodec_find_decoder(mapping->av_codec);
39+
if (decoder_codec == NULL)
40+
{
41+
vod_log_error(VOD_LOG_WARN, log, 0,
42+
"audio_decoder_process_init: failed to get %s decoder, audio decoding for this codec is disabled", mapping->name);
43+
}
2144

22-
initialized = TRUE;
45+
decoder_codecs[mapping->vod_codec] = decoder_codec;
46+
}
2347
}
2448

2549
static vod_status_t
2650
audio_decoder_init_decoder(
2751
audio_decoder_state_t* state,
2852
media_info_t* media_info)
2953
{
54+
const AVCodec *decoder_codec;
3055
AVCodecContext* decoder;
3156
int avrc;
3257

33-
if (media_info->codec_id != VOD_CODEC_ID_AAC)
58+
decoder_codec = decoder_codecs[media_info->codec_id];
59+
if (decoder_codec == NULL)
3460
{
3561
vod_log_error(VOD_LOG_ERR, state->request_context->log, 0,
3662
"audio_decoder_init_decoder: codec id %uD not supported", media_info->codec_id);
@@ -89,13 +115,6 @@ audio_decoder_init(
89115
input_frame_t* cur_frame;
90116
vod_status_t rc;
91117

92-
if (!initialized)
93-
{
94-
vod_log_error(VOD_LOG_ERR, request_context->log, 0,
95-
"audio_decoder_init: module failed to initialize successfully");
96-
return VOD_UNEXPECTED;
97-
}
98-
99118
state->request_context = request_context;
100119

101120
// init the decoder

vod/filters/audio_encoder.c

+13-28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "audio_encoder.h"
22
#include "audio_filter.h"
3+
#include "../mp4/mp4_defs.h"
34

45
// constants
56
#define AUDIO_ENCODER_BITS_PER_SAMPLE (16)
@@ -14,31 +15,14 @@ typedef struct
1415

1516
// globals
1617
static const AVCodec *encoder_codec = NULL;
17-
static bool_t initialized = FALSE;
18+
static enum AVSampleFormat audio_encoder_format = AV_SAMPLE_FMT_NONE;
1819

1920
static char* aac_encoder_names[] = {
2021
"libfdk_aac",
2122
"aac",
2223
NULL
2324
};
2425

25-
26-
static bool_t
27-
audio_encoder_is_format_supported(const AVCodec *codec, enum AVSampleFormat sample_fmt)
28-
{
29-
const enum AVSampleFormat *p;
30-
31-
for (p = codec->sample_fmts; *p != AV_SAMPLE_FMT_NONE; p++)
32-
{
33-
if (*p == sample_fmt)
34-
{
35-
return TRUE;
36-
}
37-
}
38-
39-
return FALSE;
40-
}
41-
4226
void
4327
audio_encoder_process_init(vod_log_t* log)
4428
{
@@ -66,14 +50,7 @@ audio_encoder_process_init(vod_log_t* log)
6650
}
6751
}
6852

69-
if (!audio_encoder_is_format_supported(encoder_codec, AUDIO_ENCODER_INPUT_SAMPLE_FORMAT))
70-
{
71-
vod_log_error(VOD_LOG_WARN, log, 0,
72-
"audio_encoder_process_init: encoder does not support the required input format, audio encoding is disabled");
73-
return;
74-
}
75-
76-
initialized = TRUE;
53+
audio_encoder_format = *encoder_codec->sample_fmts;
7754
}
7855

7956
vod_status_t
@@ -87,7 +64,7 @@ audio_encoder_init(
8764
AVCodecContext* encoder;
8865
int avrc;
8966

90-
if (!initialized)
67+
if (audio_encoder_format == AV_SAMPLE_FMT_NONE)
9168
{
9269
vod_log_error(VOD_LOG_ERR, request_context->log, 0,
9370
"audio_encoder_init: module failed to initialize successfully");
@@ -113,7 +90,7 @@ audio_encoder_init(
11390

11491
state->encoder = encoder;
11592

116-
encoder->sample_fmt = AUDIO_ENCODER_INPUT_SAMPLE_FORMAT;
93+
encoder->sample_fmt = audio_encoder_format;
11794
encoder->time_base.num = 1;
11895
encoder->time_base.den = params->timescale;
11996
encoder->sample_rate = params->sample_rate;
@@ -326,6 +303,8 @@ audio_encoder_update_media_info(
326303
return VOD_UNEXPECTED;
327304
}
328305

306+
media_info->format = FORMAT_MP4A;
307+
media_info->codec_id = VOD_CODEC_ID_AAC;
329308
media_info->timescale = encoder->time_base.den;
330309
media_info->bitrate = encoder->bit_rate;
331310

@@ -357,3 +336,9 @@ audio_encoder_update_media_info(
357336

358337
return VOD_OK;
359338
}
339+
340+
enum AVSampleFormat
341+
audio_encoder_get_format()
342+
{
343+
return audio_encoder_format;
344+
}

vod/filters/audio_encoder.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
#include "../media_format.h"
66
#include <libavcodec/avcodec.h>
77

8-
// constants
9-
#define AUDIO_ENCODER_INPUT_SAMPLE_FORMAT (AV_SAMPLE_FMT_S16)
10-
118
//typedefs
129
typedef struct
1310
{
@@ -45,4 +42,6 @@ vod_status_t audio_encoder_update_media_info(
4542
void* context,
4643
media_info_t* media_info);
4744

45+
enum AVSampleFormat audio_encoder_get_format();
46+
4847
#endif // __AUDIO_ENCODER_H__

vod/filters/audio_filter.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@
3333

3434
// typedefs
3535
typedef struct {
36-
enum AVSampleFormat format;
3736
void (*free)(void* context);
3837
size_t (*get_frame_size)(void* context);
3938
vod_status_t (*write)(void* context, AVFrame* frame);
4039
vod_status_t(*flush)(void* context);
4140
vod_status_t(*update_media_info)(void* context, media_info_t* media_info);
41+
enum AVSampleFormat (*get_format)();
4242
} audio_filter_encoder_t;
4343

4444
typedef struct
@@ -58,21 +58,21 @@ typedef struct
5858

5959
// constants
6060
static audio_filter_encoder_t libav_encoder = {
61-
AUDIO_ENCODER_INPUT_SAMPLE_FORMAT,
6261
audio_encoder_free,
6362
audio_encoder_get_frame_size,
6463
audio_encoder_write_frame,
6564
audio_encoder_flush,
6665
audio_encoder_update_media_info,
66+
audio_encoder_get_format,
6767
};
6868

6969
static audio_filter_encoder_t volume_map_encoder = {
70-
VOLUME_MAP_INPUT_SAMPLE_FORMAT,
7170
NULL,
7271
NULL,
7372
volume_map_encoder_write_frame,
7473
NULL,
7574
volume_map_encoder_update_media_info,
75+
volume_map_encoder_get_format,
7676
};
7777

7878
#endif
@@ -394,7 +394,7 @@ audio_filter_init_sink(
394394
}
395395

396396
// configure the buffer sink
397-
out_sample_fmts[0] = sink->encoder->format;
397+
out_sample_fmts[0] = sink->encoder->get_format();
398398
out_sample_fmts[1] = -1;
399399
avrc = av_opt_set_int_list(
400400
sink->buffer_sink,

vod/filters/volume_map.c

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "../write_buffer.h"
55

66
// constants
7+
#define VOLUME_MAP_INPUT_SAMPLE_FORMAT (AV_SAMPLE_FMT_FLTP)
78
#define RMS_LEVEL_PRECISION (100)
89
#define RMS_LEVEL_FORMAT "%uD.%02uD\n"
910

@@ -447,3 +448,9 @@ volume_map_writer_process(void* context)
447448
}
448449
}
449450
}
451+
452+
enum AVSampleFormat
453+
volume_map_encoder_get_format()
454+
{
455+
return VOLUME_MAP_INPUT_SAMPLE_FORMAT;
456+
}

vod/filters/volume_map.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
#include "../media_set.h"
66
#include <libavcodec/avcodec.h>
77

8-
// constants
9-
#define VOLUME_MAP_INPUT_SAMPLE_FORMAT (AV_SAMPLE_FMT_FLTP)
10-
118
// audio filter encoder functions
129
vod_status_t volume_map_encoder_init(
1310
request_context_t* request_context,
@@ -35,4 +32,6 @@ vod_status_t volume_map_writer_init(
3532
vod_status_t volume_map_writer_process(
3633
void* state);
3734

35+
enum AVSampleFormat volume_map_encoder_get_format();
36+
3837
#endif // __VOLUME_MAP_H__

0 commit comments

Comments
 (0)