summaryrefslogtreecommitdiff
path: root/plugins/yuv4mpeg/mpa_common.c
blob: 16bcd0300464218a390b1c356b1b4c579595beb4 (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*****************************************************************
 * 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 <config.h>
#include <unistd.h> 
#include <pthread.h>
#include <sys/signal.h>

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

#include "mpa_common.h"

#define LOG_DOMAIN "mp2enc"

static const bg_parameter_info_t parameters[] =
  {
    {
      .name =        "bitrate",
      .long_name =   TRS("Bitrate (kbps)"),
      .type =        BG_PARAMETER_INT,
      .val_default = GAVL_VALUE_INIT_INT(224),
      .val_min =     GAVL_VALUE_INIT_INT(32),
      .val_max =     GAVL_VALUE_INIT_INT(448),
    },
    {
      .name =        "layer",
      .long_name =   TRS("Layer (1 or 2)"),
      .type =        BG_PARAMETER_INT,
      .val_default = GAVL_VALUE_INIT_INT(2),
      .val_min =     GAVL_VALUE_INIT_INT(1),
      .val_max =     GAVL_VALUE_INIT_INT(2),
      .help_string = TRS("Audio layer"),
    },
    {
      .name =        "vcd",
      .long_name =   TRS("VCD Compatible"),
      .type =        BG_PARAMETER_CHECKBUTTON,
      .val_default = GAVL_VALUE_INIT_INT(1),
      .help_string = TRS("Make VCD compliant output. This forces layer II, 224 kbps and 44.1 KHz stereo"),
    },
    { /* End of parameters */ }
  };

const bg_parameter_info_t * bg_mpa_get_parameters()
  {
  return parameters;
  }
  

void bg_mpa_set_parameter(void * data, const char * name,
                          const gavl_value_t * val)
  {
  bg_mpa_common_t * com = data;
  
  if(!name)
    {
    return;
    }
  
  if(!strcmp(name, "bitrate"))
    com->bitrate = val->v.i;
  else if(!strcmp(name, "vcd"))
    com->vcd = val->v.i;
  else if(!strcmp(name, "layer"))
    com->layer = val->v.i;
  }

static char * bg_mpa_make_commandline(bg_mpa_common_t * com,
                                      const char * filename)
  {
  char * ret;
  char * mp2enc_path;
  char * tmp_string;

  if(!bg_search_file_exec("mp2enc", &mp2enc_path))
    {
    bg_log(BG_LOG_ERROR, LOG_DOMAIN,  "Cannot find mp2enc executable");
    return NULL;
    }

  /* path & audio format and verbosity */
  ret = bg_sprintf("%s -R %d,%d,16 -v 0", mp2enc_path,
                   com->format.samplerate,com->format.num_channels);

  /* Check for VCD compatibility */

  if(com->vcd)
    tmp_string = bg_sprintf(" -V -b %d", com->bitrate);
  else
    tmp_string = bg_sprintf(" -b %d -l %d -r %d",
                            com->bitrate, com->layer,
                            com->format.samplerate);
  ret = gavl_strcat(ret, tmp_string);
  free(tmp_string);

  /* Output file */
  
  tmp_string = bg_sprintf(" -o \"%s\"", filename);
  ret = gavl_strcat(ret, tmp_string);
  free(tmp_string);
  
  return ret;
  }

static const int bitrates[2][15] = {
  {0,32,64,96,128,160,192,224,256,288,320,352,384,416,448},
  {0,32,48,56,64,80,96,112,128,160,192,224,256,320,384},
  //  {0,32,40,48,56,64,80,96,112,128,160,192,224,256,320}
};

static int get_bitrate(int in_rate, int layer, int channels,
                       int vcd)
  {
  int i;
  int diff;
  int min_diff = 1000000;
  int min_i = -1;
  int ret = 0;
  
  for(i = 0; i < 15; i++)
    {
    if(bitrates[layer-1][i] == in_rate)
      {
      ret = bitrates[layer-1][i];
      break;
      }
    else
      {
      diff = abs(in_rate - bitrates[layer-1][i]);
      if(diff < min_diff)
        {
        min_diff = diff;
        min_i = i;
        }
      }
    }
  if(!ret)
    {
    ret = bitrates[layer-1][min_i];
    }

  /* Sanity checks */
  if((layer == 2) && (channels == 1) && (ret > 192))
    ret = 192;

  if(vcd)
    {
    if(channels == 2)
      {
      switch(ret)
        {
        case  128:
        case  192:
        case  224:
        case  384:
          break;
        default:
          ret = 224;
        }
      }
    else if(channels == 1)
      {
      switch(ret)
        {
        case 64:
        case 96:
        case 192:
          break;
        default:
          ret = 96;
        }
      }
    }
  if(ret != in_rate)
    {
    bg_log(BG_LOG_WARNING, LOG_DOMAIN,
           "Bitrate %d kbps unsupported, switching to %d kbps", in_rate, ret);
    }
  return ret;
  }

static int samplerates[] =
  {
#if 0
    /* MPEG-2.5 */
    8000,
    11025,
    12000,
    /* MPEG-2 */
    16000,
    22050,
    24000,
#endif
    /* MPEG-1 */
    32000,
    44100,
    48000
  };

static int get_samplerate(int in_rate, int vcd)
  {
  int i;
  int diff;
  int min_diff = 1000000;
  int min_i = -1;
  int ret = 0;
  
  if(vcd)
    ret = 44100;

  else
    {
    for(i = 0; i < sizeof(samplerates)/sizeof(samplerates[0]); i++)
      {
      if(samplerates[i] == in_rate)
        ret = in_rate;
      else
        {
        diff = abs(in_rate - samplerates[i]);
        if(diff < min_diff)
          {
          min_diff = diff;
          min_i = i;
          }
        }
      }
    if(!ret)
      ret = samplerates[min_i];
    }

  if(ret != in_rate)
    bg_log(BG_LOG_WARNING, LOG_DOMAIN,
           "Samplerate %d unsupported, switching to %d", in_rate, ret);
  return ret;
  }



static const char * extension_layer1  = "mpa";
static const char * extension_layer2  = "mp2";

const char * bg_mpa_get_extension(bg_mpa_common_t * mpa)
  {
  if(mpa->ci && (mpa->ci->id == GAVL_CODEC_ID_MP2))
    return extension_layer2;
  else if(mpa->layer == 1)
    return extension_layer1;
  else
    return extension_layer2;
  }

void bg_mpa_set_format(bg_mpa_common_t * com,
                       const gavl_audio_format_t * format)
  {
  gavl_audio_format_copy(&com->format, format);

  com->format.sample_format = GAVL_SAMPLE_S16;
  com->format.interleave_mode = GAVL_INTERLEAVE_ALL;

  if(com->format.num_channels > 2)
    {
    com->format.num_channels = 2;
    com->format.channel_locations[0] = GAVL_CHID_NONE;
    gavl_set_channel_setup(&com->format);
    }
  }

void bg_mpa_get_format(bg_mpa_common_t * com, gavl_audio_format_t * format)
  {
  gavl_audio_format_copy(format, &com->format);
  }

static gavl_sink_status_t
write_audio_func(void * priv,
                 gavl_audio_frame_t * frame)
  {
  bg_mpa_common_t * com = priv;
  int bytes = 2 * com->format.num_channels * frame->valid_samples;
  
  if(write(com->mp2enc->stdin_fd, frame->samples.s_16, bytes) < bytes)
    return GAVL_SINK_ERROR;
  return GAVL_SINK_OK;
  }

static gavl_sink_status_t
write_packet_func(void * priv,
                  gavl_packet_t * packet)
  {
  bg_mpa_common_t * com = priv;
  if(fwrite(packet->data, 1, packet->data_len, com->out) < packet->data_len)
    return GAVL_SINK_ERROR;
  return GAVL_SINK_OK;
  }


int bg_mpa_start(bg_mpa_common_t * com, const char * filename)
  {
  sigset_t newset;
  char * commandline;

  if(com->ci)
    {
    com->out = fopen(filename, "wb");
    com->psink = gavl_packet_sink_create(NULL, write_packet_func, com);
    }
  else
    {
    /* Block SIGPIPE */
    sigemptyset(&newset);
    sigaddset(&newset, SIGPIPE);
    pthread_sigmask(SIG_BLOCK, &newset, &com->oldset);
  
    /* Adjust Samplerate */
    com->format.samplerate = get_samplerate(com->format.samplerate, com->vcd);
    
    /* Adjust bitrate */
  
    com->bitrate = get_bitrate(com->bitrate, com->layer,
                               com->format.num_channels, com->vcd);

    commandline = 
      bg_mpa_make_commandline(com, filename);
    if(!commandline)
      {
      return 0;
      }
    com->mp2enc = bg_subprocess_create(commandline, 1, 0, 0);
    if(!com->mp2enc)
      return 0;
    free(commandline);
    com->sink = gavl_audio_sink_create(NULL, write_audio_func, com, &com->format);
    }
  return 1;
  }

int bg_mpa_close(bg_mpa_common_t * com)
  {
  int ret = 1;
  if(com->mp2enc)
    {
    if(bg_subprocess_close(com->mp2enc))
      ret = 0;
    }
  if(com->out)
    fclose(com->out);
  pthread_sigmask(SIG_SETMASK, &com->oldset, NULL);

  if(com->sink)
    {
    gavl_audio_sink_destroy(com->sink);
    com->sink = NULL;
    }
  if(com->psink)
    {
    gavl_packet_sink_destroy(com->psink);
    com->psink = NULL;
    }

  return ret;
  }

void bg_mpa_set_ci(bg_mpa_common_t * com, const gavl_compression_info_t * ci)
  {
  com->ci = ci;
  }