File: faac_codec.c

package info (click to toggle)
gmerlin-encoders 2.0.0~svn6278-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 2,076 kB
  • sloc: ansic: 15,264; sh: 662; makefile: 364; sed: 16
file content (492 lines) | stat: -rw-r--r-- 13,540 bytes parent folder | download | duplicates (3)
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*****************************************************************
 * 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 <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

#include <faac.h>

#include <config.h>

#include <gmerlin_encoders.h>

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

#include <gavl/metatags.h>


#define LOG_DOMAIN "faac"

#include "faac_codec.h"

#define FAAC_DELAY 1024

struct bg_faac_s
  {
  faacEncHandle enc;
  faacEncConfigurationPtr enc_config;
  
  gavl_audio_sink_t * asink;
  gavl_packet_sink_t * psink;

  gavl_audio_frame_t * frame;

  gavl_packet_t p;
  
  gavl_audio_format_t fmt;

  int64_t in_pts;
  int64_t out_pts;
  
  /* Config stuff */
  unsigned int mpegVersion;
  unsigned int aacObjectType;
  unsigned int allowMidside;
  unsigned int useTns;
  unsigned long bitRate;
  unsigned long quantqual;
  int shortctl;
   
  };


bg_faac_t * bg_faac_create(void)
  {
  bg_faac_t * ret = calloc(1, sizeof(*ret));

  

  return ret;
  }

static const bg_parameter_info_t parameters[] =
  {
    {
      .name =        "basic",
      .long_name =   TRS("Basic options"),
      .type =        BG_PARAMETER_SECTION
    },
    {
      .name =        "object_type",
      .long_name =   TRS("Object type"),
      .type =        BG_PARAMETER_STRINGLIST,
      .val_default = GAVL_VALUE_INIT_STRING("mpeg4_main"),
      .multi_names = (char const *[]){ "mpeg4_lc",
                              "mpeg2_lc",
                              "mpeg4_main",
                              "mpeg4_lc",
                              "mpeg4_ltp",
                              NULL },
      .multi_labels = (char const *[]){ TRS("MPEG-2 Main profile"),
                                        TRS("MPEG-2 Low Complexity profile (LC)"),
                                        TRS("MPEG-4 Main profile"),
                                        TRS("MPEG-4 Low Complexity profile (LC)"),
                                        TRS("MPEG-4 Long Term Prediction (LTP)"),
                               NULL },
    },
    {
      .name =        "bitrate",
      .long_name =   TRS("Bitrate (kbps)"),
      .type =        BG_PARAMETER_INT,
      .val_min =     GAVL_VALUE_INIT_INT(0),
      .val_max =     GAVL_VALUE_INIT_INT(1000),
      .help_string = TRS("Average bitrate (0: VBR based on quality)"),
    },
    {
      .name =        "quality",
      .long_name =   TRS("Quality"),
      .type =        BG_PARAMETER_SLIDER_INT,
      .val_min =     GAVL_VALUE_INIT_INT(10),
      .val_max =     GAVL_VALUE_INIT_INT(500),
      .val_default = GAVL_VALUE_INIT_INT(100),
      .help_string = TRS("Quantizer quality"),
    },
    {
      .name =        "advanced",
      .long_name =   TRS("Advanced options"),
      .type =        BG_PARAMETER_SECTION
    },
    {
      .name =        "block_types",
      .long_name =   TRS("Block types"),
      .type =        BG_PARAMETER_STRINGLIST,
      .val_default = GAVL_VALUE_INIT_STRING("Both"),
      .multi_names = (char const *[]){ "Both",
                              "No short",
                              "No long",
                              NULL },
      .multi_labels = (char const *[]){ TRS("Both"),
                               TRS("No short"),
                               TRS("No long"),
                               NULL },
    },
    {
      .name =        "tns",
      .type =        BG_PARAMETER_CHECKBUTTON,
      .long_name =   TRS("Use temporal noise shaping"),
      .val_default = GAVL_VALUE_INIT_INT(0)
    },
    {
      .name =        "no_midside",
      .long_name =   TRS("Don\'t use mid/side coding"),
      .type =        BG_PARAMETER_CHECKBUTTON,
      .val_default = GAVL_VALUE_INIT_INT(0)
    },
    { /* End of parameters */ }
  };

const bg_parameter_info_t * bg_faac_get_parameters(bg_faac_t * ctx)
  {
  return parameters;
  }

void bg_faac_set_parameter(bg_faac_t * ctx, const char * name,
                           const gavl_value_t * v)
  {
  if(!name)
    {
    return;
    }
  else if(!strcmp(name, "object_type"))
    {
    if(!strcmp(v->v.str, "mpeg2_main"))
      {
      ctx->mpegVersion = MPEG2;
      ctx->aacObjectType = MAIN;
      }
    else if(!strcmp(v->v.str, "mpeg2_lc"))
      {
      ctx->mpegVersion = MPEG2;
      ctx->aacObjectType = LOW;
      }
    else if(!strcmp(v->v.str, "mpeg4_main"))
      {
      ctx->mpegVersion = MPEG4;
      ctx->aacObjectType = MAIN;
      }
    else if(!strcmp(v->v.str, "mpeg4_lc"))
      {
      ctx->mpegVersion = MPEG4;
      ctx->aacObjectType = LOW;
      }
    else if(!strcmp(v->v.str, "mpeg4_ltp"))
      {
      ctx->mpegVersion = MPEG4;
      ctx->aacObjectType = LTP;
      }
    }
  else if(!strcmp(name, "bitrate"))
    {
    ctx->bitRate = v->v.i * 1000;
    }
  else if(!strcmp(name, "quality"))
    {
    ctx->quantqual = v->v.i;
    }
  else if(!strcmp(name, "block_types"))
    {
    if(!strcmp(v->v.str, "Both"))
      {
      ctx->shortctl = SHORTCTL_NORMAL;
      }
    else if(!strcmp(v->v.str, "No short"))
      {
      ctx->shortctl = SHORTCTL_NOSHORT;
      }
    else if(!strcmp(v->v.str, "No long"))
      {
      ctx->shortctl = SHORTCTL_NOLONG;
      }
    }
  else if(!strcmp(name, "tns"))
    {
    ctx->useTns = v->v.i;
    }
  else if(!strcmp(name, "no_midside"))
    {
    ctx->allowMidside = !v->v.i;
    }

  }

static int flush_audio(bg_faac_t * ctx)
  {
  int i, imax;
  int bytes_encoded;
  int num_samples;
  
  gavl_packet_reset(&ctx->p);
  
  /* First, we must scale the samples to -32767 .. 32767 */

  imax = ctx->frame->valid_samples * ctx->fmt.num_channels;
  
  for(i = 0; i < imax; i++)
    ctx->frame->samples.f[i] *= 32767.0;
  
  /* Encode the stuff */


  num_samples = ctx->frame->valid_samples ?
    (ctx->fmt.samples_per_frame * ctx->fmt.num_channels) : 0;

  //fprintf(stderr, "Encode %d\n", num_samples);
  
  bytes_encoded = faacEncEncode(ctx->enc,
                                (int32_t*)ctx->frame->samples.f,
                                num_samples,
                                ctx->p.data, ctx->p.data_alloc);

  ctx->p.data_len = bytes_encoded;

  /* Mute sets valid samples to samples_per_frame!! */
  gavl_audio_frame_mute(ctx->frame, &ctx->fmt);
  
  ctx->frame->valid_samples = 0;
  
  /* Write this to the file */

  if(bytes_encoded)
    {
    
    
    ctx->p.pts = ctx->out_pts;

    ctx->p.duration = ctx->fmt.samples_per_frame;

    if(ctx->p.pts + ctx->p.duration > ctx->in_pts)
      ctx->p.duration = ctx->in_pts - ctx->p.pts;
    
    ctx->out_pts += ctx->p.duration;
    
//    fprintf(stderr, "Got AAC packet\n");
//    gavl_packet_dump(&ctx->p);
    
    if(gavl_packet_sink_put_packet(ctx->psink, &ctx->p) != GAVL_SINK_OK)
      return -1;
    }
  return bytes_encoded;
  }


static gavl_sink_status_t
write_audio_func_faac(void * data, gavl_audio_frame_t * frame)
  {
  int samples_done = 0;
  int samples_copied;
  bg_faac_t * ctx = data;

  if(ctx->in_pts == GAVL_TIME_UNDEFINED)
    {
    ctx->in_pts = frame->timestamp;
    ctx->out_pts = ctx->in_pts - FAAC_DELAY;
    }
  
  while(samples_done < frame->valid_samples)
    {

    /* Copy frame into our buffer */
    
    samples_copied =
      gavl_audio_frame_copy(&ctx->fmt,
                            ctx->frame,                                                 /* dst */
                            frame,                                                       /* src */
                            ctx->frame->valid_samples,                                  /* dst_pos */
                            samples_done,                                                /* src_pos */
                            ctx->fmt.samples_per_frame - ctx->frame->valid_samples, /* dst_size */
                            frame->valid_samples - samples_done);                        /* src_size */
    
    samples_done += samples_copied;
    ctx->frame->valid_samples += samples_copied;
    
    /* Encode buffer */

    if(ctx->frame->valid_samples == ctx->fmt.samples_per_frame)
      {
      if(flush_audio(ctx) < 0)
        return GAVL_SINK_ERROR;
      }
    }
  
  ctx->in_pts += ctx->frame->valid_samples;
  return GAVL_SINK_OK;
  }


gavl_audio_sink_t * bg_faac_open(bg_faac_t * ctx,
                                 gavl_compression_info_t * ci,
                                 gavl_audio_format_t * fmt,
                                 gavl_dictionary_t * m)
  {
  unsigned long input_samples;
  unsigned long output_bytes;
    
  /* Create encoder handle and get configuration */

  ctx->enc = faacEncOpen(fmt->samplerate,
                         fmt->num_channels,
                         &input_samples,
                         &output_bytes);
  
  ctx->enc_config = faacEncGetCurrentConfiguration(ctx->enc);
  ctx->enc_config->inputFormat = FAAC_INPUT_FLOAT;

  /* Decide output format: If ci == NULL we output ADTS */

  if(ci)
    ctx->enc_config->outputFormat = 0; // raw
  else      
    ctx->enc_config->outputFormat = 1; // ADTS

  /* Set config */

  ctx->enc_config->mpegVersion   = ctx->mpegVersion;
  ctx->enc_config->aacObjectType = ctx->aacObjectType;
  ctx->enc_config->allowMidside  = ctx->allowMidside;
  ctx->enc_config->useTns        = ctx->useTns;
  ctx->enc_config->bitRate       = ctx->bitRate / fmt->num_channels;
  ctx->enc_config->quantqual     = ctx->quantqual;
  ctx->enc_config->shortctl      = ctx->shortctl;
  
  /* Copy and adjust format */

  fmt->interleave_mode = GAVL_INTERLEAVE_ALL;
  fmt->sample_format   = GAVL_SAMPLE_FLOAT;
  fmt->samples_per_frame = input_samples / fmt->num_channels;

  switch(fmt->num_channels)
    {
    case 1:
      fmt->channel_locations[0] = GAVL_CHID_FRONT_CENTER;
      break;
    case 2:
      fmt->channel_locations[0] = GAVL_CHID_FRONT_LEFT;
      fmt->channel_locations[1] = GAVL_CHID_FRONT_RIGHT;
      break;
    case 3:
      fmt->channel_locations[0] = GAVL_CHID_FRONT_CENTER;
      fmt->channel_locations[1] = GAVL_CHID_FRONT_LEFT;
      fmt->channel_locations[2] = GAVL_CHID_FRONT_RIGHT;
      break;
    case 4:
      fmt->channel_locations[0] = GAVL_CHID_FRONT_CENTER;
      fmt->channel_locations[1] = GAVL_CHID_FRONT_LEFT;
      fmt->channel_locations[2] = GAVL_CHID_FRONT_RIGHT;
      fmt->channel_locations[3] = GAVL_CHID_REAR_CENTER;
      break;
    case 5:
      fmt->channel_locations[0] = GAVL_CHID_FRONT_CENTER;
      fmt->channel_locations[1] = GAVL_CHID_FRONT_LEFT;
      fmt->channel_locations[2] = GAVL_CHID_FRONT_RIGHT;
      fmt->channel_locations[3] = GAVL_CHID_REAR_LEFT;
      fmt->channel_locations[4] = GAVL_CHID_REAR_RIGHT;
      break;
    case 6:
      fmt->channel_locations[0] = GAVL_CHID_FRONT_CENTER;
      fmt->channel_locations[1] = GAVL_CHID_FRONT_LEFT;
      fmt->channel_locations[2] = GAVL_CHID_FRONT_RIGHT;
      fmt->channel_locations[3] = GAVL_CHID_REAR_LEFT;
      fmt->channel_locations[4] = GAVL_CHID_REAR_RIGHT;
      fmt->channel_locations[5] = GAVL_CHID_LFE;
      break;

    }
  
  gavl_packet_alloc(&ctx->p, output_bytes);

  if(!faacEncSetConfiguration(ctx->enc, ctx->enc_config))
    { 
    gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN,  "faacEncSetConfiguration failed");
    return NULL;
    }
    
  gavl_audio_format_copy(&ctx->fmt, fmt);
  ctx->frame = gavl_audio_frame_create(&ctx->fmt);
  
  ctx->asink =
    gavl_audio_sink_create(NULL, write_audio_func_faac, ctx, &ctx->fmt);

  /* Initialize compression info */
  if(ci)
    {
    unsigned long SizeOfDecoderSpecificInfo;
    ci->id = GAVL_CODEC_ID_AAC;

    faacEncGetDecoderSpecificInfo(ctx->enc, &ci->global_header,
                                  &SizeOfDecoderSpecificInfo);
    ci->global_header_len = SizeOfDecoderSpecificInfo;
    ci->pre_skip = FAAC_DELAY;
    gavl_dictionary_set_string_nocopy(m, GAVL_META_SOFTWARE,
                            bg_sprintf("libfaac %s", ctx->enc_config->name));
    
    }
  
  ctx->in_pts = GAVL_TIME_UNDEFINED;
  ctx->out_pts = GAVL_TIME_UNDEFINED;
  return ctx->asink;
  }

void bg_faac_set_packet_sink(bg_faac_t * ctx,
                             gavl_packet_sink_t * psink)
  {
  ctx->psink = psink;
  }

void bg_faac_destroy(bg_faac_t * ctx)
  {
  int result;
  /* Flush remaining audio data */

  if(ctx->enc)
    {
    while(1)
      {
      result = flush_audio(ctx);
      if(result <= 0)
        break;
      }
    }
  
  if(ctx->enc)
    {
    faacEncClose(ctx->enc);
    ctx->enc = NULL;
    }

  gavl_packet_free(&ctx->p);
  
  if(ctx->frame)
    gavl_audio_frame_destroy(ctx->frame);
  
  if(ctx->asink)
    {
    gavl_audio_sink_destroy(ctx->asink);
    ctx->asink = NULL;
    }
    
  free(ctx);
  }