File: audio.c

package info (click to toggle)
gmerlin-avdecoder 2.0.0~svn6298~dfsg0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 6,112 kB
  • sloc: ansic: 94,580; sh: 705; makefile: 705; awk: 43; sed: 16
file content (612 lines) | stat: -rw-r--r-- 15,961 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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
/*****************************************************************
 * gmerlin-avdecoder - a general purpose multimedia decoding library
 *
 * 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 <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <avdec_private.h>
#include <parser.h>
#include <bsf.h>

#define LOG_DOMAIN "audio"

int bgav_num_audio_streams(bgav_t * bgav, int track)
  {
  return bgav->tt->tracks[track]->num_audio_streams;
  }

const gavl_audio_format_t * bgav_get_audio_format(bgav_t *  bgav, int stream)
  {
  bgav_stream_t * s;

  if(!(s = bgav_track_get_audio_stream(bgav->tt->cur, stream)))
    return NULL;
  
  return s->data.audio.format;
  }

const gavl_audio_format_t * bgav_get_audio_format_t(bgav_t *  bgav, int track, int stream)
  {
  bgav_stream_t * s;

  if(!(s = bgav_track_get_audio_stream(bgav->tt->tracks[track], stream)))
    return NULL;
  
  
  return s->data.audio.format;
  }

int bgav_set_audio_stream(bgav_t * b, int stream, bgav_stream_action_t action)
  {
  bgav_stream_t * s;
  if(!(s = bgav_track_get_audio_stream(b->tt->cur, stream)))
    return 0;
  s->action = action;
  return 1;
  }

static gavl_source_status_t get_frame(void * sp, gavl_audio_frame_t ** frame)
  {
  bgav_stream_t * s = sp;
  
  if(!(s->flags & STREAM_HAVE_FRAME) &&
     !s->data.audio.decoder->decode_frame(s))
    {
    s->flags |= STREAM_EOF_C;
    return GAVL_SOURCE_EOF;
    }
  s->flags &= ~STREAM_HAVE_FRAME; 
  s->data.audio.frame->timestamp = s->out_time;
  s->out_time += s->data.audio.frame->valid_samples;
  *frame = s->data.audio.frame;
  return GAVL_SOURCE_OK;
  }

int bgav_audio_start(bgav_stream_t * s)
  {
  bgav_audio_decoder_t * dec;
  
  if((s->flags & (STREAM_PARSE_FULL|STREAM_PARSE_FRAME)) &&
     !s->data.audio.parser)
    {
    s->data.audio.parser = bgav_audio_parser_create(s);
    if(!s->data.audio.parser)
      {
      gavl_log(GAVL_LOG_WARNING, LOG_DOMAIN,
               "No audio parser found for fourcc %c%c%c%c (0x%08x)",
               (s->fourcc & 0xFF000000) >> 24,
               (s->fourcc & 0x00FF0000) >> 16,
               (s->fourcc & 0x0000FF00) >> 8,
               (s->fourcc & 0x000000FF),
               s->fourcc);
      return 0;
      }
    
    /* Start the parser */
    
    if(bgav_stream_peek_packet_read(s, NULL, 1) != GAVL_SOURCE_OK)
      {
      gavl_log(GAVL_LOG_WARNING, LOG_DOMAIN,
               "EOF while initializing audio parser");
      return 0;
      }
    
    s->index_mode = INDEX_MODE_SIMPLE;
    }

  if((s->action == BGAV_STREAM_READRAW) &&
     (s->flags & STREAM_FILTER_PACKETS) && !s->bsf)
    {
    s->bsf = bgav_bsf_create(s);
    if(bgav_stream_peek_packet_read(s, NULL, 1) != GAVL_SOURCE_OK)
      {
      gavl_log(GAVL_LOG_WARNING, LOG_DOMAIN,
               "EOF while initializing bitstream filter");
      return 0;
      }
    }
  
  if(s->stats.pts_start == GAVL_TIME_UNDEFINED)
    {
    bgav_packet_t * p = NULL;
    char tmp_string[128];
    
    if(bgav_stream_peek_packet_read(s, &p, 1) != GAVL_SOURCE_OK)
      {
      gavl_log(GAVL_LOG_WARNING, LOG_DOMAIN,
               "EOF while getting start time");
      }
    s->stats.pts_start = p->pts;
    sprintf(tmp_string, "%" PRId64, s->stats.pts_start);
    gavl_log(GAVL_LOG_DEBUG, LOG_DOMAIN, "Got initial audio timestamp: %s",
             tmp_string);
    } /* Else */
  //  else if(s->data.audio.pre_skip > 0)
  //    s->stats.pts_start = -s->data.audio.pre_skip;

  s->out_time = s->stats.pts_start;
  
  if(!s->timescale && s->data.audio.format->samplerate)
    s->timescale = s->data.audio.format->samplerate;
  
  if(s->action == BGAV_STREAM_DECODE)
    {
    dec = bgav_find_audio_decoder(s->fourcc);
    if(!dec)
      {
      if(!(s->fourcc & 0xffff0000))
        gavl_log(GAVL_LOG_WARNING, LOG_DOMAIN,
                 "No audio decoder found for WAV ID 0x%04x", s->fourcc);
      else
        gavl_log(GAVL_LOG_WARNING, LOG_DOMAIN,
                 "No audio decoder found for fourcc %c%c%c%c (0x%08x)",
                 (s->fourcc & 0xFF000000) >> 24,
                 (s->fourcc & 0x00FF0000) >> 16,
                 (s->fourcc & 0x0000FF00) >> 8,
                 (s->fourcc & 0x000000FF),
                 s->fourcc);
      return 0;
      }
    s->data.audio.decoder = dec;
    s->data.audio.frame = gavl_audio_frame_create(NULL);
    
    if(!dec->init(s))
      return 0;
    
    if(!s->timescale)
      s->timescale = s->data.audio.format->samplerate;

    //    s->out_time -= s->data.audio.pre_skip;
    }

  if(s->data.audio.bits_per_sample)
    gavl_dictionary_set_int(s->m, GAVL_META_AUDIO_BITS ,
                          s->data.audio.bits_per_sample);


  //  if(s->container_bitrate == GAVL_BITRATE_VBR)
  //    gavl_dictionary_set_string(&s->m, GAVL_META_BITRATE,
  //                      "VBR");
  else if(s->codec_bitrate)
    gavl_dictionary_set_int(s->m, GAVL_META_BITRATE,
                          s->codec_bitrate);
  else if(s->container_bitrate)
    gavl_dictionary_set_int(s->m, GAVL_META_BITRATE,
                          s->container_bitrate);

  if(s->action == BGAV_STREAM_DECODE)
    s->data.audio.source =
      gavl_audio_source_create(get_frame, s,
                               GAVL_SOURCE_SRC_ALLOC | s->src_flags,
                               s->data.audio.format);
#if 1
  if(s->action == BGAV_STREAM_READRAW)
    s->psrc =
      gavl_packet_source_create_audio(bgav_stream_read_packet_func, // get_packet,
                                      s, GAVL_SOURCE_SRC_ALLOC,
                                      &s->ci, s->data.audio.format);
#endif
  
  //  if(s->data.audio.pre_skip && s->data.audio.source)
  //    gavl_audio_source_skip_src(s->data.audio.source, s->data.audio.pre_skip);
  
  return 1;
  }

void bgav_audio_stop(bgav_stream_t * s)
  {
  if(s->data.audio.decoder)
    {
    s->data.audio.decoder->close(s);
    s->data.audio.decoder = NULL;
    }
  if(s->data.audio.parser)
    {
    bgav_audio_parser_destroy(s->data.audio.parser);
    s->data.audio.parser = NULL;
    }
  if(s->data.audio.frame)
    {
    gavl_audio_frame_null(s->data.audio.frame);
    gavl_audio_frame_destroy(s->data.audio.frame);
    s->data.audio.frame = NULL;
    }
  if(s->data.audio.source)
    {
    gavl_audio_source_destroy(s->data.audio.source);
    s->data.audio.source = NULL;
    }
  }

const char * bgav_get_audio_description(bgav_t * b, int stream)
  {
  bgav_stream_t * s;
  
  if(!(s = bgav_track_get_audio_stream(b->tt->cur, stream)))
    return NULL;
  
  return gavl_dictionary_get_string(s->m, GAVL_META_FORMAT);
  }

const char * bgav_get_audio_info(bgav_t * b, int stream)
  {
  bgav_stream_t * s;
  
  if(!(s = bgav_track_get_audio_stream(b->tt->cur, stream)))
    return NULL;

  return gavl_dictionary_get_string(s->m,
                                    GAVL_META_LABEL);
  }

const bgav_metadata_t *
bgav_get_audio_metadata(bgav_t * b, int stream)
  {
  bgav_stream_t * s;
  
  if(!(s = bgav_track_get_audio_stream(b->tt->cur, stream)))
    return NULL;

  return s->m;
  }

const bgav_metadata_t *
bgav_get_audio_metadata_t(bgav_t * b, int track, int stream)
  {
  bgav_stream_t * s;
  
  if(!(s = bgav_track_get_audio_stream(b->tt->tracks[track], stream)))
    return NULL;

  return s->m;
  }


const char * bgav_get_audio_language(bgav_t * b, int stream)
  {
  bgav_stream_t * s;
  
  if(!(s = bgav_track_get_audio_stream(b->tt->cur, stream)))
    return NULL;
  
  return gavl_dictionary_get_string(s->m, GAVL_META_LANGUAGE);
  }

int bgav_get_audio_bitrate(bgav_t * bgav, int stream)
  {
  bgav_stream_t * s;
  
  if(!(s = bgav_track_get_audio_stream(bgav->tt->cur, stream)))
    return 0;
  
  if(s->codec_bitrate)
    return s->codec_bitrate;
  else if(s->container_bitrate)
    return s->container_bitrate;
  else
    return 0;
  }

int bgav_read_audio(bgav_t * b, gavl_audio_frame_t * frame,
                    int stream, int num_samples)
  {
  bgav_stream_t * s;
  
  if(!(s = bgav_track_get_audio_stream(b->tt->cur, stream)))
    return 0;
  return gavl_audio_source_read_samples(s->data.audio.source,
                                        frame, num_samples);
  }

void bgav_audio_dump(bgav_stream_t * s)
  {
  bgav_dprintf("  Bits per sample:   %d\n", s->data.audio.bits_per_sample);
  bgav_dprintf("  Block align:       %d\n", s->data.audio.block_align);
  bgav_dprintf("  Pre skip:          %d\n", s->ci.pre_skip);
  bgav_dprintf("Format:\n");
  gavl_audio_format_dump(s->data.audio.format);
  }


void bgav_audio_resync(bgav_stream_t * s)
  {
  if(s->data.audio.frame)
    s->data.audio.frame->valid_samples = 0;
  
  if(s->out_time == GAVL_TIME_UNDEFINED)
    {
    s->out_time =
      gavl_time_rescale(s->timescale,
                        s->data.audio.format->samplerate,
                        STREAM_GET_SYNC(s));
    }
  
  if(s->data.audio.parser)
    {
    bgav_packet_t * p = NULL;
    bgav_audio_parser_reset(s->data.audio.parser,
                            GAVL_TIME_UNDEFINED, s->out_time);

    if(bgav_stream_peek_packet_read(s, &p, 1) != GAVL_SOURCE_OK)
      {
      gavl_log(GAVL_LOG_WARNING, LOG_DOMAIN,
               "EOF while resyncing");
      }
    else
      s->out_time = p->pts;
    }
  
  if(s->data.audio.decoder &&
     s->data.audio.decoder->resync)
    s->data.audio.decoder->resync(s);
  
  if(s->data.audio.source)
    gavl_audio_source_reset(s->data.audio.source);

  //  fprintf(stderr, "audio resync %"PRId64"\n", s->out_time);

  }

int bgav_audio_skipto(bgav_stream_t * s, int64_t * t, int scale)
  {
  int64_t num_samples;
  // int samples_skipped = 0;  
  int64_t skip_time;

  skip_time = gavl_time_rescale(scale,
                                s->data.audio.format->samplerate,
                                *t);
  
  num_samples = skip_time - s->out_time;
  
  if(num_samples < 0)
    {
    gavl_log(GAVL_LOG_WARNING, LOG_DOMAIN,
             "Cannot skip backwards: Stream time: %"PRId64" skip time: %"PRId64" difference: %"PRId64,
             s->out_time, skip_time, num_samples);
    return 1;
    }
  
  gavl_audio_source_skip(s->data.audio.source, num_samples);
  return 1;
  }

static uint32_t alaw_fourccs[] =
  {
  BGAV_MK_FOURCC('a', 'l', 'a', 'w'),
  BGAV_MK_FOURCC('A', 'L', 'A', 'W'),
  BGAV_WAVID_2_FOURCC(0x06),
  0x00
  };

static uint32_t ulaw_fourccs[] =
  {
  BGAV_MK_FOURCC('u', 'l', 'a', 'w'),
  BGAV_MK_FOURCC('U', 'L', 'A', 'W'),
  BGAV_WAVID_2_FOURCC(0x07),
  0x00
  };

static uint32_t mp2_fourccs[] =
  {
  BGAV_MK_FOURCC('.', 'm', 'p', '2'),
  BGAV_WAVID_2_FOURCC(0x0050),
  0x00
  };

static uint32_t mp3_fourccs[] =
  {
  BGAV_MK_FOURCC('.', 'm', 'p', '3'),
  BGAV_WAVID_2_FOURCC(0x0055),
  0x00
  };

static uint32_t ac3_fourccs[] =
  {
    BGAV_WAVID_2_FOURCC(0x2000),
    BGAV_MK_FOURCC('.', 'a', 'c', '3'),
    BGAV_MK_FOURCC('d', 'n', 'e', 't'), 
    BGAV_MK_FOURCC('a', 'c', '-', '3'),
    0x00
  };

static uint32_t vorbis_fourccs[] =
  {
    BGAV_MK_FOURCC('V','B','I','S'),
    0x00
  };

static uint32_t aac_fourccs[] =
  {
    BGAV_MK_FOURCC('m','p','4','a'),
    0x00
  };

static uint32_t flac_fourccs[] =
  {
    BGAV_MK_FOURCC('F','L','A','C'),
    0x00
  };

static uint32_t opus_fourccs[] =
  {
    BGAV_MK_FOURCC('O','P','U','S'),
    0x00
  };

static uint32_t speex_fourccs[] =
  {
    BGAV_MK_FOURCC('S','P','E','X'),
    0x00
  };

static uint32_t adts_fourccs[] =
  {
    BGAV_MK_FOURCC('A','D','T','S'),
    0x00
  };

static uint32_t dts_fourccs[] =
  {
    BGAV_MK_FOURCC('d', 't', 's', ' '),
    0x00,
  };

int bgav_get_audio_compression_info(bgav_t * bgav, int stream,
                                    gavl_compression_info_t * ret)
  {
  int need_header = 0;
  int need_bitrate = 1;
  gavl_codec_id_t id = GAVL_CODEC_ID_NONE;

  bgav_stream_t * s;
  
  if(!(s = bgav_track_get_audio_stream(bgav->tt->cur, stream)))
    return 0;
  
  if(ret)
    memset(ret, 0, sizeof(*ret));
  
  if(s->flags & STREAM_GOT_CI)
    {
    if(ret)
      gavl_compression_info_copy(ret, &s->ci);
    return 1;
    }
  else if(s->flags & STREAM_GOT_NO_CI)
    return 0;

  bgav_track_get_compression(bgav->tt->cur);
  
  if(bgav_check_fourcc(s->fourcc, alaw_fourccs))
    id = GAVL_CODEC_ID_ALAW;
  else if(bgav_check_fourcc(s->fourcc, ulaw_fourccs))
    id = GAVL_CODEC_ID_ULAW;
  else if(bgav_check_fourcc(s->fourcc, ac3_fourccs))
    id = GAVL_CODEC_ID_AC3;
  else if(bgav_check_fourcc(s->fourcc, mp2_fourccs))
    id = GAVL_CODEC_ID_MP2;
  else if(bgav_check_fourcc(s->fourcc, mp3_fourccs))
    id = GAVL_CODEC_ID_MP3;
  else if(bgav_check_fourcc(s->fourcc, dts_fourccs))
    id = GAVL_CODEC_ID_DTS;
  else if(bgav_check_fourcc(s->fourcc, aac_fourccs))
    {
    id = GAVL_CODEC_ID_AAC;
    need_header = 1;
    need_bitrate = 0;
    }
  else if(bgav_check_fourcc(s->fourcc, speex_fourccs))
    {
    id = GAVL_CODEC_ID_SPEEX;
    need_header = 1;
    need_bitrate = 0;
    }
  else if(bgav_check_fourcc(s->fourcc, vorbis_fourccs))
    {
    id = GAVL_CODEC_ID_VORBIS;
    need_bitrate = 0;
    need_header = 1;
    }
  else if(bgav_check_fourcc(s->fourcc, flac_fourccs))
    {
    id = GAVL_CODEC_ID_FLAC;
    need_bitrate = 0;
    need_header = 1;
    }
  else if(bgav_check_fourcc(s->fourcc, opus_fourccs))
    {
    id = GAVL_CODEC_ID_OPUS;
    need_bitrate = 0;
    need_header = 1;
    }
  else if(bgav_check_fourcc(s->fourcc, adts_fourccs) &&
          (s->flags & STREAM_FILTER_PACKETS))
    {
    id = GAVL_CODEC_ID_AAC;
    need_header = 1;
    need_bitrate = 0;
    }
  
  if(id == GAVL_CODEC_ID_NONE)
    {
    gavl_log(GAVL_LOG_WARNING, LOG_DOMAIN,
             "Cannot output compressed audio stream %d: Unsupported codec",
             stream+1);
    s->flags |= STREAM_GOT_NO_CI;
    return 0;
    }
  if(need_header && !s->ci.global_header_len)
    {
    gavl_log(GAVL_LOG_WARNING, LOG_DOMAIN,
             "Cannot output compressed audio stream %d: Global header missing",
             stream+1);
    s->flags |= STREAM_GOT_NO_CI;
    return 0;
    }
  if(need_bitrate && !s->container_bitrate && !s->codec_bitrate)
    {
    gavl_log(GAVL_LOG_WARNING, LOG_DOMAIN,
             "Cannot output compressed audio stream %d: No bitrate specified",
             stream+1);
    s->flags |= STREAM_GOT_NO_CI;
    return 0;
    }
  s->ci.id = id;
  
  
  if(s->codec_bitrate)
    s->ci.bitrate = s->codec_bitrate;
  else if(s->container_bitrate)
    s->ci.bitrate = s->container_bitrate;

  if(ret)
    gavl_compression_info_copy(ret, &s->ci);
  
  s->flags |= STREAM_GOT_CI;
  return 1;
  }

int bgav_read_audio_packet(bgav_t * bgav, int stream, gavl_packet_t * p)
  {
  bgav_stream_t * s;
  
  if(!(s = bgav_track_get_audio_stream(bgav->tt->cur, stream)))
    return 0;
  return (gavl_packet_source_read_packet(s->psrc, &p) == GAVL_SOURCE_OK);
  }

gavl_audio_source_t * bgav_get_audio_source(bgav_t * bgav, int stream)
  {
  bgav_stream_t * s;
  if(!(s = bgav_track_get_audio_stream(bgav->tt->cur, stream)))
    return NULL;
  return s->data.audio.source;
  }

gavl_packet_source_t * bgav_get_audio_packet_source(bgav_t * bgav, int stream)
  {
  bgav_stream_t * s;
  if(!(s = bgav_track_get_audio_stream(bgav->tt->cur, stream)))
    return NULL;
  return s->psrc;
  }