]> git.sesse.net Git - ffmpeg/blob - libavcodec/ac3enc_combined.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libavcodec / ac3enc_combined.c
1
2 #include "libavutil/opt.h"
3 #include "libavutil/samplefmt.h"
4 #include "avcodec.h"
5 #include "ac3.h"
6
7 typedef struct CombineContext{
8     AVClass *av_class;                      ///< AVClass used for AVOption
9     AC3EncOptions options;                  ///< encoding options
10     void *ctx;
11     AVCodec *codec;
12 }CombineContext;
13
14 #define OFFSET(param) offsetof(CombineContext, options.param)
15 #define AC3ENC_PARAM (AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM)
16
17 #define AC3ENC_TYPE_AC3_FIXED   0
18 #define AC3ENC_TYPE_AC3         1
19 #define AC3ENC_TYPE_EAC3        2
20
21 #define AC3ENC_TYPE 12354
22
23 /* common option values */
24 #define AC3ENC_OPT_NONE            -1
25 #define AC3ENC_OPT_AUTO            -1
26 #define AC3ENC_OPT_OFF              0
27 #define AC3ENC_OPT_ON               1
28 #define AC3ENC_OPT_NOT_INDICATED    0
29 #define AC3ENC_OPT_MODE_ON          1
30 #define AC3ENC_OPT_MODE_OFF         2
31
32 /* specific option values */
33 #define AC3ENC_OPT_LARGE_ROOM       1
34 #define AC3ENC_OPT_SMALL_ROOM       2
35 #define AC3ENC_OPT_DOWNMIX_LTRT     1
36 #define AC3ENC_OPT_DOWNMIX_LORO     2
37 #define AC3ENC_OPT_ADCONV_STANDARD  0
38 #define AC3ENC_OPT_ADCONV_HDCD      1
39
40 #include "ac3enc_opts_template.c"
41
42 static AVClass ac3enc_class = { "AC-3 Encoder", av_default_item_name,
43                                 eac3_options, LIBAVUTIL_VERSION_INT };
44
45 static av_cold AVCodec *get_codec(enum AVSampleFormat s){
46 #if CONFIG_AC3_FIXED_ENCODER
47     if(s==AV_SAMPLE_FMT_S16) return &ff_ac3_fixed_encoder;
48 #endif
49 #if CONFIG_AC3_FLOAT_ENCODER
50     if(s==AV_SAMPLE_FMT_FLT) return &ff_ac3_float_encoder;
51 #endif
52     return NULL;
53 }
54
55
56 static av_cold int encode_init(AVCodecContext *avctx)
57 {
58     CombineContext *c= avctx->priv_data;
59     int ret;
60     int offset= (uint8_t*)&c->options - (uint8_t*)c;
61
62     c->codec= get_codec(avctx->sample_fmt);
63     if(!c->codec){
64         av_log(avctx, AV_LOG_ERROR, "Unsupported sample format\n");
65         return -1;
66     }
67     c->ctx= av_mallocz(c->codec->priv_data_size);
68     memcpy((uint8_t*)c->ctx + offset, &c->options, (uint8_t*)&c->ctx - (uint8_t*)&c->options);
69     FFSWAP(void *,avctx->priv_data, c->ctx);
70     ret= c->codec->init(avctx);
71     FFSWAP(void *,avctx->priv_data, c->ctx);
72     return ret;
73 }
74
75 static int encode_frame(AVCodecContext *avctx, unsigned char *frame,
76                         int buf_size, void *data)
77 {
78     CombineContext *c= avctx->priv_data;
79     int ret;
80
81     FFSWAP(void *,avctx->priv_data, c->ctx);
82     ret= c->codec->encode(avctx, frame, buf_size, data);
83     FFSWAP(void *,avctx->priv_data, c->ctx);
84     return ret;
85 }
86
87 static av_cold int encode_close(AVCodecContext *avctx)
88 {
89     CombineContext *c= avctx->priv_data;
90     int ret;
91
92     FFSWAP(void *,avctx->priv_data, c->ctx);
93     ret= c->codec->close(avctx);
94     FFSWAP(void *,avctx->priv_data, c->ctx);
95     return ret;
96 }
97
98 AVCodec ff_ac3_encoder = {
99     "ac3",
100     AVMEDIA_TYPE_AUDIO,
101     CODEC_ID_AC3,
102     sizeof(CombineContext),
103     encode_init,
104     encode_frame,
105     encode_close,
106     NULL,
107     .sample_fmts = (const enum AVSampleFormat[]){
108 #if CONFIG_AC3_FLOAT_ENCODER
109         AV_SAMPLE_FMT_FLT,
110 #endif
111 #if CONFIG_AC3_FIXED_ENCODER
112         AV_SAMPLE_FMT_S16,
113 #endif
114         AV_SAMPLE_FMT_NONE},
115     .long_name = NULL_IF_CONFIG_SMALL("ATSC A/52A (AC-3)"),
116     .priv_class = &ac3enc_class,
117     .channel_layouts = ff_ac3_channel_layouts,
118 };