summaryrefslogtreecommitdiff
path: root/plugins/yuv4mpeg/e_mpegaudio.c
blob: 5e469d3d51020abba20ad823d2351a8a44ea6dd0 (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
/*****************************************************************
 * gmerlin-encoders - encoder plugins for gmerlin
 *
 * Copyright (c) 2001 - 2012 Members of the Gmerlin project
 * gmerlin-general@lists.sourceforge.net
 * http://gmerlin.sourceforge.net
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * *****************************************************************/

#include <string.h>
#include <stdlib.h>
#include <config.h>

#include <gmerlin/translation.h>
#include <gmerlin/plugin.h>
#include <gmerlin/pluginfuncs.h>
#include <gmerlin/utils.h>
#include <gmerlin/subprocess.h>
#include <gmerlin/log.h>

#define LOG_DOMAIN "e_mpegaudio"


#include <gmerlin_encoders.h>
#include "mpa_common.h"

typedef struct
  {
  char * filename;

  bg_mpa_common_t com;

  bg_encoder_callbacks_t * cb;

  const gavl_compression_info_t * ci;
  } e_mpa_t;

static void * create_mpa()
  {
  e_mpa_t * ret;
  ret = calloc(1, sizeof(*ret));
  return ret;
  }

static void destroy_mpa(void * priv)
  {
  e_mpa_t * mpa;
  mpa = priv;

  free(mpa);
  }

static void set_callbacks_mpa(void * data, bg_encoder_callbacks_t * cb)
  {
  e_mpa_t * mpa = data;
  mpa->cb = cb;
  }


static void set_parameter_mpa(void * data, const char * name,
                              const gavl_value_t * v)
  {
  e_mpa_t * mpa;
  mpa = data;
  if(!name)
    {
    return;
    }
  bg_mpa_set_parameter(&mpa->com, name, v);
  }

static int open_mpa(void * data, const char * filename,
                    const gavl_dictionary_t * metadata,
                    const gavl_chapter_list_t * chapter_list)
  {
  e_mpa_t * mpa;
  mpa = data;

  mpa->filename =
    bg_filename_ensure_extension(filename,
                                 bg_mpa_get_extension(&mpa->com));

  if(!bg_encoder_cb_create_output_file(mpa->cb, mpa->filename))
    return 0;
  
  return 1;
  }

static int add_audio_stream_mpa(void * data,
                                const gavl_dictionary_t * m,
                                const gavl_audio_format_t * format)
  {
  e_mpa_t * mpa;

  mpa = data;
  bg_mpa_set_format(&mpa->com, format);
  return 0;
  }

static int add_audio_stream_compressed_mpa(void * data,
                                           const gavl_dictionary_t * m,
                                           const gavl_audio_format_t * format,
                                           const gavl_compression_info_t * ci)
  {
  e_mpa_t * mpa;
  mpa = data;
  bg_mpa_set_ci(&mpa->com, ci);
  return 0;
  }

static int start_mpa(void * data)
  {
  int result;
  e_mpa_t * e = data;
  result = bg_mpa_start(&e->com, e->filename);
  if(!result)
    gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN, "Cannot find mp2enc executable");
  return result;
  }

static int writes_compressed_audio_mpa(void * priv,
                                       const gavl_audio_format_t * format,
                                       const gavl_compression_info_t * info)
  {
  switch(info->id)
    {
    case GAVL_CODEC_ID_MP2:
      return 1;
    default:
      break;
    }
  return 0;
  }


static gavl_audio_sink_t * get_audio_sink_mpa(void * data, int stream)
  {
  e_mpa_t * mpa;
  mpa = data;
  return mpa->com.sink;
  }

static gavl_packet_sink_t * get_audio_packet_sink_mpa(void * data, int stream)
  {
  e_mpa_t * mpa;
  mpa = data;
  return mpa->com.psink;
  }

static int close_mpa(void * data, int do_delete)
  {
  int ret = 1;
  e_mpa_t * mpa;
  mpa = data;

  if(!bg_mpa_close(&mpa->com))
    ret = 0;

  if(do_delete)
    {
    gavl_log(GAVL_LOG_INFO, LOG_DOMAIN, "Removing %s", mpa->filename);
    remove(mpa->filename);
    }
  if(mpa->filename)
    free(mpa->filename);
  return ret;
  }


static const bg_parameter_info_t * get_parameters_mpa(void * data)
  {
  return bg_mpa_get_parameters();
  }


const bg_encoder_plugin_t the_plugin =
  {
    .common =
    {
      BG_LOCALE,
      .name =            "e_mpegaudio",       /* Unique short name */
      .long_name =       TRS("MPEG-1 layer 1/2 audio encoder"),
      .description =     TRS("Encoder for elementary MPEG-1 layer 1/2 audio streams.\
 Based on mjpegtools (http://mjpeg.sourceforge.net)."),
      .type =            BG_PLUGIN_ENCODER_AUDIO,
      .flags =           BG_PLUGIN_FILE,
      .priority =        5,
      .create =            create_mpa,
      .destroy =           destroy_mpa,
      .get_parameters =    get_parameters_mpa,
      .set_parameter =     set_parameter_mpa,
    },
    .max_audio_streams =   1,
    .max_video_streams =   0,
    
    .writes_compressed_audio = writes_compressed_audio_mpa,
    .set_callbacks =       set_callbacks_mpa,
    
    .open =                open_mpa,
    .add_audio_stream =    add_audio_stream_mpa,
    .add_audio_stream_compressed =    add_audio_stream_compressed_mpa,
    .get_audio_sink   =    get_audio_sink_mpa,
    .get_audio_packet_sink   =    get_audio_packet_sink_mpa,
    
    .start =               start_mpa,
    .close =               close_mpa
  };

/* Include this into all plugin modules exactly once
   to let the plugin loader obtain the API version */
BG_GET_PLUGIN_API_VERSION;