~ubuntu-branches/ubuntu/trusty/libdv/trusty

« back to all changes in this revision

Viewing changes to libdv/audio.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Kobras
  • Date: 2004-07-19 12:19:44 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 warty)
  • Revision ID: james.westby@ubuntu.com-20040719121944-17vuryc01yeyx8hf
Tags: 0.103-2
* debian/rules: Provide separate doc directory for libdv4-dev.
* debian/libdv4-dev.links: No longer symlink doc dir to the one
  from libdv4.
* debian/NEWS: Only install into libdv4-dev. Closes: #259694

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* 
 
1
/*
2
2
 *  audio.c
3
3
 *
4
4
 *     Copyright (C) Charles 'Buck' Krasic - January 2001
7
7
 *  codec.
8
8
 *
9
9
 *  libdv is free software; you can redistribute it and/or modify it
10
 
 *  under the terms of the GNU General Public License as published by
11
 
 *  the Free Software Foundation; either version 2, or (at your
 
10
 *  under the terms of the GNU Lesser Public License as published by
 
11
 *  the Free Software Foundation; either version 2.1, or (at your
12
12
 *  option) any later version.
13
13
 *   
14
14
 *  libdv is distributed in the hope that it will be useful, but
15
15
 *  WITHOUT ANY WARRANTY; without even the implied warranty of
16
16
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
 
 *  General Public License for more details.
 
17
 *  Lesser Public License for more details.
18
18
 *   
19
 
 *  You should have received a copy of the GNU General Public License
20
 
 *  along with GNU Make; see the file COPYING.  If not, write to
 
19
 *  You should have received a copy of the GNU Lesser Public License
 
20
 *  along with libdv; see the file COPYING.  If not, write to
21
21
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
22
22
 *
23
23
 *  The libdv homepage is http://libdv.sourceforge.net/.  
24
24
 */
25
25
 
26
 
#include <dv_types.h>
 
26
#if HAVE_CONFIG_H
 
27
# include <config.h>
 
28
#endif
27
29
 
28
30
#include <stdio.h>
29
31
#include <math.h>
 
32
#include <string.h>
30
33
 
 
34
#include "dv.h"
31
35
#include "util.h"
32
36
#include "audio.h"
33
37
 
 
38
int dv_is_normal_speed (dv_decoder_t*);
 
39
 
34
40
/** @file
35
41
 *  @ingroup decoder
36
42
 *  @brief Audio routines for decoding
85
91
  { 1944, 1786, 1296 }, /* 50 fields (PAL) */
86
92
};
87
93
 
88
 
static int frequency[3] = {
89
 
  48000, 44100, 32000
 
94
static int frequency[8] = {
 
95
  48000, 44100, 32000, -3, -4, -5, -6, -7
90
96
};
91
97
 
92
98
 
134
140
} /* dv_audio_popt_callback  */
135
141
#endif /* HAVE_LIBPOPT */
136
142
 
137
 
static int 
 
143
static int
138
144
dv_audio_samples_per_frame(dv_aaux_as_t *dv_aaux_as, int freq) {
139
145
  int result = -1;
140
146
  int col;
217
223
  return(result);
218
224
} /* dv_upsample */
219
225
 
 
226
/* ---------------------------------------------------------------------------
 
227
 */
 
228
void dv_test12bit_conv (void)
 
229
{
 
230
    int i;
 
231
 
 
232
  for (i = 0; i < 0x7ff; ++i)
 
233
  {
 
234
    fprintf (stderr, " (%5d,%5d,0x%08x,0x%08x) -> (%5d,%5d,0x%08x,0x%08x) (%d)\n\r",
 
235
             i, -i, i, -i,
 
236
             dv_upsample (i), dv_upsample(-i), dv_upsample (i), dv_upsample (-i),
 
237
             dv_upsample (-i) + dv_upsample (i));
 
238
  }
 
239
}
 
240
 
 
241
/* ---------------------------------------------------------------------------
 
242
 */
220
243
dv_audio_t *
221
244
dv_audio_new(void)
222
245
{
223
246
  dv_audio_t *result;
224
 
  
 
247
 
225
248
  if(!(result = (dv_audio_t *)calloc(1,sizeof(dv_audio_t)))) goto no_mem;
226
249
 
227
250
#if HAVE_LIBPOPT
228
 
  result->option_table[DV_AUDIO_OPT_FREQUENCY] = (struct poptOption){ 
229
 
    longName:   "frequency", 
230
 
    shortName:  'f', 
231
 
    argInfo:    POPT_ARG_INT, 
 
251
  result->option_table[DV_AUDIO_OPT_FREQUENCY] = (struct poptOption){
 
252
    longName:   "frequency",
 
253
    shortName:  'f',
 
254
    argInfo:    POPT_ARG_INT,
232
255
    arg:        &result->arg_audio_frequency,
233
256
    descrip:    "audio frequency: 0=autodetect [default], 1=32 kHz, 2=44.1 kHz, 3=48 kHz",
234
257
    argDescrip: "(0|1|2|3)"
235
258
  }; /* freq */
236
259
 
237
 
  result->option_table[DV_AUDIO_OPT_QUANTIZATION] = (struct poptOption){ 
238
 
    longName:   "quantization", 
239
 
    shortName:  'Q', 
240
 
    argInfo:    POPT_ARG_INT, 
 
260
  result->option_table[DV_AUDIO_OPT_QUANTIZATION] = (struct poptOption){
 
261
    longName:   "quantization",
 
262
    shortName:  'Q',
 
263
    argInfo:    POPT_ARG_INT,
241
264
    arg:        &result->arg_audio_quantization,
242
265
    descrip:    "audio quantization: 0=autodetect [default], 1=12 bit, 2=16bit",
243
266
    argDescrip: "(0|1|2)"
244
267
  }; /* quant */
245
268
 
246
 
  result->option_table[DV_AUDIO_OPT_EMPHASIS] = (struct poptOption){ 
247
 
    longName:   "emphasis", 
248
 
    shortName:  'e', 
249
 
    argInfo:    POPT_ARG_INT, 
 
269
  result->option_table[DV_AUDIO_OPT_EMPHASIS] = (struct poptOption){
 
270
    longName:   "emphasis",
 
271
    shortName:  'e',
 
272
    argInfo:    POPT_ARG_INT,
250
273
    arg:        &result->arg_audio_emphasis,
251
274
    descrip:    "first-order preemphasis of 50/15 us: 0=autodetect [default], 1=on, 2=off",
252
275
    argDescrip: "(0|1|2)"
253
276
  }; /* quant */
254
277
 
 
278
  result->option_table[DV_AUDIO_OPT_CHAN_MIX] = (struct poptOption) {
 
279
    longName:   "audio-mix",
 
280
    argInfo:    POPT_ARG_INT,
 
281
    arg:        &result->arg_mixing_level,
 
282
    descrip:    "mixing level between 1st and 2nd channel for 32kHz 12bit. 0 [default]",
 
283
    argDescrip: "(-16 .. 16)",
 
284
  };
 
285
 
255
286
  result->option_table[DV_AUDIO_OPT_CALLBACK] = (struct poptOption){
256
287
    argInfo: POPT_ARG_CALLBACK|POPT_CBFLAG_POST,
257
288
    arg:     dv_audio_popt_callback,
259
290
  }; /* callback */
260
291
 
261
292
#endif /* HAVE_LIBPOPT */
 
293
  /* dv_test12bit_conv (); */
262
294
  return(result);
263
295
 
264
296
 no_mem:
265
297
  return(result);
266
298
} /* dv_audio_new */
267
299
 
268
 
void 
269
 
dv_dump_aaux_as(void *buffer, int ds, int audio_dif) 
 
300
void
 
301
dv_dump_aaux_as(void *buffer, int ds, int audio_dif)
270
302
{
271
303
  dv_aaux_as_t *dv_aaux_as;
272
304
 
286
318
    printf(", Sampling ");
287
319
    printf("%.1f kHz", (float)frequency[dv_aaux_as->pc4.smp] / 1000.0);
288
320
 
289
 
    printf(" (%d samples, %d fields)", 
 
321
    printf(" (%d samples, %d fields)",
290
322
           dv_audio_samples_per_frame(dv_aaux_as,frequency[dv_aaux_as->pc4.smp]),
291
323
           (dv_aaux_as->pc3.system ? 50 : 60));
292
324
 
293
325
    printf(", Quantization %d bits", quantization[dv_aaux_as->pc4.qu]);
294
 
    
 
326
 
295
327
    printf(", Emphasis %s\n", (dv_aaux_as->pc4.ef ? "off" : "on"));
296
328
 
297
329
  } else {
302
334
 
303
335
} /* dv_dump_aaux_as */
304
336
 
 
337
/* ---------------------------------------------------------------------------
 
338
 */
 
339
void
 
340
dv_dump_audio_header (dv_decoder_t *decoder, int ds, uint8_t *inbuf)
 
341
{
 
342
    int     i;
 
343
    uint8_t *p;
 
344
 
 
345
  fprintf (stderr, " ");
 
346
  for (i = 0, p = inbuf + 80 * 16 * ((ds&1) ? 0: 3); i < 8; i++, p++)
 
347
  {
 
348
    fprintf (stderr, " %02x ", *p);
 
349
  }
 
350
 
 
351
  for (i = 0, p = inbuf + 80 * 16 * ((ds & 1) ? 1: 4); i < 8; i++, p++)
 
352
  {
 
353
    fprintf (stderr, " %02x ", *p);
 
354
  }
 
355
  fprintf (stderr, "\n");
 
356
 
 
357
}
 
358
 
 
359
/* ---------------------------------------------------------------------------
 
360
 */
305
361
int
306
 
dv_parse_audio_header(dv_decoder_t *decoder, uint8_t *inbuf)
 
362
dv_parse_audio_header(dv_decoder_t *decoder, const uint8_t *inbuf)
307
363
{
308
 
  dv_audio_t *audio = decoder->audio;
309
 
  dv_aaux_as_t *dv_aaux_as   = (dv_aaux_as_t *) (inbuf + 80*6+80*16*3 + 3);
310
 
  dv_aaux_asc_t *dv_aaux_asc = (dv_aaux_asc_t *)(inbuf + 80*6+80*16*4 + 3);
311
 
  int normal_speed = FALSE;
 
364
  dv_audio_t    *audio = decoder->audio;
 
365
  dv_aaux_as_t  *dv_aaux_as  = (dv_aaux_as_t *) (inbuf + 80*6+80*16*3 + 3),
 
366
                *dv_aaux_as1 = NULL;
 
367
  dv_aaux_asc_t *dv_aaux_asc = (dv_aaux_asc_t *)(inbuf + 80*6+80*16*4 + 3),
 
368
                *dv_aaux_asc1 = NULL;
312
369
 
313
370
  if((dv_aaux_as->pc0 != 0x50) || (dv_aaux_asc->pc0 != 0x51)) goto bad_id;
314
371
 
315
372
  audio->max_samples =  max_samples[dv_aaux_as->pc3.system][dv_aaux_as->pc4.smp];
316
373
  /* For now we assume that 12bit = 4 channels */
317
374
  if(dv_aaux_as->pc4.qu > 1) goto unsupported_quantization;
318
 
  /*audio->num_channels = (dv_aaux_as->pc4.qu+1) * 2; // TODO verify this is right with known 4-channel input */
319
 
 
320
 
  audio->num_channels = 2; /* TODO verify this is right with known 4-channel input */
321
 
 
 
375
  /*audio->num_channels = (dv_aaux_as->pc4.qu+1) * 2;
 
376
  // TODO verify this is right with known 4-channel input */
 
377
 
 
378
  audio->num_channels =
 
379
    audio->raw_num_channels = 2; /* TODO verify this is right with known 4-channel input */
 
380
                                 /* DONE: see below */
322
381
  switch(audio->arg_audio_frequency) {
323
382
  case 0:
324
383
    audio->frequency = frequency[dv_aaux_as->pc4.smp];
350
409
  case 0:
351
410
    if(decoder->std == e_dv_std_iec_61834) {
352
411
      audio->emphasis = (dv_aaux_as->pc4.ef == 0);
353
 
    } else if(decoder->std == e_dv_std_iec_61834) {
 
412
    } else if(decoder->std == e_dv_std_smpte_314m) {
354
413
      audio->emphasis = (dv_aaux_asc->pc1.ss == 1);
355
414
    } else {
356
415
      /* TODO: should never happen... */
364
423
    break;
365
424
  } /* switch  */
366
425
 
367
 
  audio->samples_this_frame = dv_audio_samples_per_frame(dv_aaux_as,audio->frequency);
368
 
 
369
 
  audio->aaux_as  = *dv_aaux_as;
370
 
  audio->aaux_asc = *dv_aaux_asc;
371
 
 
372
 
  if(decoder->std == e_dv_std_iec_61834) {
373
 
    normal_speed = (dv_aaux_asc->pc3.speed == 0x20);
374
 
  } else if(decoder->std == e_dv_std_iec_61834) {
375
 
    if(dv_aaux_as->pc3.system) {
376
 
      /* PAL */
377
 
      normal_speed = (dv_aaux_asc->pc3.speed == 0x64);
378
 
    } else {
379
 
      /* NTSC */
380
 
      normal_speed = (dv_aaux_asc->pc3.speed == 0x78);
381
 
    } /* else */
382
 
  } /* else */
383
 
 
384
 
  return(normal_speed); /* don't do audio if speed is not 1 */
 
426
  /* -------------------------------------------------------------------------
 
427
   * so try to detect 4 channel audio mode
 
428
   */
 
429
  if (audio -> frequency == 32000 && audio -> quantization == 12)
 
430
  {
 
431
    /* -----------------------------------------------------------------------
 
432
     * check different location PAL/NTSC + EVEN/ODD index
 
433
     * in even dif sequences as and acs info is at index 3/4
 
434
     * in odd def sequences as and asc info is at index 0/1
 
435
     * ref: SMPTE 314m, p. 16, table 15
 
436
     */
 
437
    if (dv_aaux_as -> pc3. system)
 
438
    {
 
439
      dv_aaux_as1 = (dv_aaux_as_t *) (inbuf +
 
440
                      80 * (9 * 16 + 6) * 6 + /* go 6 dif sequences ahead  */
 
441
                      80 *  6 +               /* skip 6 header blocks      */
 
442
                      80 * 16 * 3 +           /* select block 3 (1a + 15v) */
 
443
                      3);                     /* skip id bytes             */
 
444
      dv_aaux_asc1 = (dv_aaux_asc_t *) (inbuf +
 
445
                      80 * (9 * 16 + 6) * 6 + /* go 6 dif sequences ahead  */
 
446
                      80 *  6 +               /* skip 6 header blocks      */
 
447
                      80 * 16 * 4 +           /* select block 4 (1a + 15v) */
 
448
                      3);                     /* skip id bytes             */
 
449
    }
 
450
    else
 
451
    {
 
452
      dv_aaux_as1 = (dv_aaux_as_t *) (inbuf +
 
453
                      80 * (9 * 16 + 6) * 6 + /* go 6 dif sequences ahead  */
 
454
                      80 *  6 +               /* skip 6 header blocks      */
 
455
                      80 * 16 * 0 +           /* select block 0 (1a + 15v) */
 
456
                      3);                     /* skip id bytes             */
 
457
      dv_aaux_asc1 = (dv_aaux_asc_t *) (inbuf +
 
458
                      80 * (9 * 16 + 6) * 6 + /* go 6 dif sequences ahead  */
 
459
                      80 *  6 +               /* skip 6 header blocks      */
 
460
                      80 * 16 * 1 +           /* select block 1 (1a + 15v) */
 
461
                      3);                     /* skip id bytes             */
 
462
    }
 
463
    if (dv_aaux_as1 -> pc2. audio_mode != 0xf)
 
464
    {
 
465
      audio -> raw_num_channels = 4;
 
466
      audio -> aaux_as1  = *dv_aaux_as1;
 
467
      audio -> aaux_asc1 = *dv_aaux_asc1;
 
468
    }
 
469
  }
 
470
  audio -> samples_this_frame =
 
471
    audio -> raw_samples_this_frame [0] = dv_audio_samples_per_frame (dv_aaux_as,
 
472
                                                                      audio -> frequency);
 
473
  if (audio -> raw_num_channels == 4)
 
474
  {
 
475
    audio -> raw_samples_this_frame [1] = dv_audio_samples_per_frame (dv_aaux_as1,
 
476
                                                                      audio -> frequency);
 
477
  } else {
 
478
    audio -> raw_samples_this_frame [1] = 0;
 
479
  }
 
480
  audio -> aaux_as  = *dv_aaux_as;
 
481
  audio -> aaux_asc = *dv_aaux_asc;
 
482
 
 
483
  return dv_is_normal_speed(decoder); /* don't do audio if speed is not 1 */
385
484
 
386
485
 bad_id:
387
486
  return(FALSE);
388
 
  
 
487
 
389
488
 unsupported_quantization:
390
 
  fprintf(stderr, "libdv(%s):  Malformrmed AAUX AS? pc4.qu == %d\n", 
 
489
  fprintf(stderr, "libdv(%s):  Malformrmed AAUX AS? pc4.qu == %d\n",
391
490
          __FUNCTION__, audio->aaux_as.pc4.qu);
392
491
  return(FALSE);
393
492
 
394
493
} /* dv_parse_audio_header */
395
494
 
396
495
int
397
 
dv_update_num_samples(dv_audio_t *dv_audio, uint8_t *inbuf) {
 
496
dv_update_num_samples(dv_audio_t *dv_audio, const uint8_t *inbuf) {
398
497
 
399
498
  dv_aaux_as_t *dv_aaux_as = (dv_aaux_as_t *)(inbuf + 80*6+80*16*3 + 3);
400
499
 
401
500
  if(dv_aaux_as->pc0 != 0x50) goto bad_id;
402
 
  dv_audio->samples_this_frame = dv_audio_samples_per_frame(dv_aaux_as,dv_audio->frequency);
 
501
  dv_audio->samples_this_frame =
 
502
  dv_audio->raw_samples_this_frame[0]=
 
503
              dv_audio_samples_per_frame (dv_aaux_as, dv_audio -> frequency);
 
504
  /* TODO: update second channels bytes too */
403
505
  return(TRUE);
404
506
 
405
507
 bad_id:
410
512
/* This code originates from cdda2wav, by way of Giovanni Iachello <g.iachello@iol.it>
411
513
   to Arne Schirmacher <arne@schirmacher.de>. */
412
514
void
413
 
dv_audio_deemphasis(dv_audio_t *audio, int16_t *outbuf)
 
515
dv_audio_deemphasis(dv_audio_t *audio, int16_t **outbuf)
414
516
{
415
 
  int i;
416
 
  /* this implements an attenuation treble shelving filter 
417
 
     to undo the effect of pre-emphasis. The filter is of
418
 
     a recursive first order */
419
 
  /* static */ short lastin[2] = { 0, 0 };
420
 
  /* static */ double lastout[2] = { 0.0, 0.0 };
421
 
  short *pmm;
422
 
  /* See deemphasis.gnuplot */
423
 
  double V0     = 0.3365;
424
 
  double OMEGAG = (1./19e-6);
425
 
  double T      = (1./audio->frequency);
426
 
  double H0     = (V0-1.);
427
 
  double B      = (V0*tan((OMEGAG * T)/2.0));
428
 
  double a1     = ((B - 1.)/(B + 1.));
429
 
  double b0 = (1.0 + (1.0 - a1) * H0/2.0);
430
 
  double b1 = (a1 + (a1 - 1.0) * H0/2.0);
431
 
 
432
 
  /* For 48Khz: a1= -0.659065 b0= 0.449605 b1= -0.108670 
433
 
   * For 44.1Khz: a1=-0.62786881719628784282  b0=       0.45995451989513153057 b1=-0.08782333709141937339
434
 
   * For 32kHZ ? */
435
 
 
436
 
  for (pmm = (short *)outbuf, i=0; 
437
 
       i < audio->samples_this_frame; 
438
 
       i++) {
439
 
    lastout[0] = *pmm * b0 + lastin[0] * b1 - lastout[0] * a1;
440
 
    lastin[0] = *pmm;
441
 
    *pmm++ = lastout[0] > 0.0 ? lastout[0] + 0.5 : lastout[0] - 0.5;
442
 
  } /* for */
443
 
 
 
517
    int i, ch;
 
518
    /* this implements an attenuation treble shelving filter
 
519
       to undo the effect of pre-emphasis. The filter is of
 
520
       a recursive first order */
 
521
    short lastin;
 
522
    double lastout;
 
523
    short *pmm;
 
524
    /* See deemphasis.gnuplot */
 
525
    double V0     = 0.3365;
 
526
    double OMEGAG = (1./19e-6);
 
527
    double T  = (1./audio->frequency);
 
528
    double H0 = (V0-1.);
 
529
    double B  = (V0*tan((OMEGAG * T)/2.0));
 
530
    double a1 = ((B - 1.)/(B + 1.));
 
531
    double b0 = (1.0 + (1.0 - a1) * H0/2.0);
 
532
    double b1 = (a1 + (a1 - 1.0) * H0/2.0);
 
533
 
 
534
  if (audio->emphasis) {
 
535
    for (ch=0; ch< audio->raw_num_channels; ch++) {
 
536
      /* ---------------------------------------------------------------------
 
537
       * For 48Khz:   a1=-0.659065
 
538
       *              b0= 0.449605
 
539
       *              b1=-0.108670
 
540
       * For 44.1Khz: a1=-0.62786881719628784282
 
541
       *              b0= 0.45995451989513153057
 
542
       *              b1=-0.08782333709141937339
 
543
       * For 32kHZ ?
 
544
       */
 
545
      lastin = audio->lastin [ch];
 
546
      lastout = audio->lastout [ch];
 
547
      for (pmm = (short *)outbuf [ch], i=0;
 
548
           i < audio->raw_samples_this_frame [0]; /* TODO: check for second channel */
 
549
           i++) {
 
550
        lastout = *pmm * b0 + lastin * b1 - lastout * a1;
 
551
        lastin = *pmm;
 
552
        *pmm++ = (lastout > 0.0) ? lastout + 0.5 : lastout - 0.5;
 
553
      } /* for (pmn = .. */
 
554
      audio->lastout [ch] = lastout;
 
555
      audio->lastin [ch] = lastin;
 
556
    } /* for (ch = .. */
 
557
  } /* if (audio -> .. */
444
558
} /* dv_audio_deemphasis */
445
559
 
446
 
 
447
 
int 
448
 
dv_decode_audio_block(dv_audio_t *dv_audio, uint8_t *inbuf, int ds, int audio_dif, int16_t **outbufs) 
 
560
/* ---------------------------------------------------------------------------
 
561
 */
 
562
int
 
563
dv_decode_audio_block(dv_audio_t *dv_audio,
 
564
                      const uint8_t *inbuf,
 
565
                      int ds,
 
566
                      int audio_dif,
 
567
                      int16_t **outbufs)
449
568
{
450
569
  int channel, bp, i_base, i, stride;
451
570
  int16_t *samples, *ysamples, *zsamples;
452
571
  int16_t y,z;
453
572
  int32_t msb_y, msb_z, lsb;
454
 
  int half_ds;
 
573
  int half_ds, full_failure;
 
574
  char      err_msg1 [40],
 
575
            err_msg2 [40];
455
576
 
456
577
#if 0
457
578
  if ((inbuf[0] & 0xe0) != 0x60) goto bad_id;
476
597
    stride = 45;
477
598
  } /* else */
478
599
 
 
600
  full_failure = 0;
 
601
 
479
602
  if(dv_audio->quantization == 16) {
480
 
 
481
603
    samples = outbufs[channel];
482
 
 
483
604
    for (bp = 8; bp < 80; bp+=2) {
484
605
 
485
606
      i = i_base + (bp - 8)/2 * stride;
486
 
      samples[i] = ((int16_t)inbuf[bp] << 8) | inbuf[bp+1];
 
607
      if ((samples[i] = ((int16_t)inbuf[bp] << 8) | inbuf[bp+1]) == (int16_t) 0x8000) {
 
608
        full_failure++;
 
609
      }
487
610
 
488
611
    } /* for */
 
612
    /* -----------------------------------------------------------------------
 
613
     * check if some or all samples in block failed
 
614
     */
 
615
    if (full_failure) {
 
616
      if (dv_audio -> error_log) {
 
617
        if (dv_get_timestamp (dv_audio -> dv_decoder, err_msg1) &&
 
618
            dv_get_recording_datetime (dv_audio -> dv_decoder, err_msg2)) {
 
619
        fprintf (dv_audio -> error_log,
 
620
                   "%s %s %s %02x %02x %02x 16 %d/36\n",
 
621
                   (full_failure == 36) ? "abf": "asf",
 
622
                   err_msg1, err_msg2,
 
623
                   inbuf [0], inbuf [1], inbuf [2],
 
624
                   full_failure);
 
625
        } else {
 
626
          fprintf (dv_audio -> error_log,
 
627
                   "# audio %s failure (16bit): "
 
628
                   "header = %02x %02x %02x\n",
 
629
                   (full_failure == 36) ? "block": "sample",
 
630
                   inbuf [0], inbuf [1], inbuf [2]);
 
631
        }
 
632
      }
 
633
      if (full_failure == 36) {
 
634
        dv_audio -> block_failure++;
 
635
      }
 
636
    }
 
637
    dv_audio -> sample_failure += full_failure;
489
638
 
490
639
  } else if(dv_audio->quantization == 12) {
491
640
 
500
649
 
501
650
      msb_y = inbuf[bp];
502
651
      msb_z = inbuf[bp+1];
503
 
      lsb   = inbuf[bp+2];  
 
652
      lsb   = inbuf[bp+2];
504
653
 
505
654
      y = ((msb_y << 4) & 0xff0) | ((lsb >> 4) & 0xf);
506
 
      if(y > 2047) y -= 4096;
507
655
      z = ((msb_z << 4) & 0xff0) | (lsb & 0xf);
508
 
      if(z > 2047) z -= 4096;
509
656
 
510
 
      ysamples[i] = dv_upsample(y); 
511
 
      zsamples[i] = dv_upsample(z);
 
657
      if(y > 2048) y -= 4096;
 
658
      if(z > 2048) z -= 4096;
 
659
      /* ---------------------------------------------------------------------
 
660
       * so check if a sample has an error value 0x800 and keep this code
 
661
       * for later correction.
 
662
       */
 
663
      if (y == 2048) {
 
664
        full_failure++;
 
665
        ysamples[i] = 0x8000;
 
666
      } else {
 
667
        ysamples[i] = dv_upsample(y);
 
668
      }
 
669
      if (z == 2048) {
 
670
        full_failure++;
 
671
        zsamples[i] = 0x8000;
 
672
      } else {
 
673
        zsamples[i] = dv_upsample(z);
 
674
      }
512
675
    } /* for  */
 
676
    /* -----------------------------------------------------------------------
 
677
     * check if some or all samples in block failed
 
678
     */
 
679
    if (full_failure) {
 
680
      if (dv_audio -> error_log) {
 
681
        if (dv_get_timestamp (dv_audio -> dv_decoder, err_msg1) &&
 
682
            dv_get_recording_datetime (dv_audio -> dv_decoder, err_msg2)) {
 
683
        fprintf (dv_audio -> error_log,
 
684
                   "%s %s %s %02x %02x %02x 12 %d/48\n",
 
685
                   (full_failure == 48) ? "abf": "asf",
 
686
                   err_msg1, err_msg2,
 
687
                   inbuf [0], inbuf [1], inbuf [2], full_failure);
 
688
        } else {
 
689
          fprintf (dv_audio -> error_log,
 
690
                   "# audio %s failure (12bit): "
 
691
                   "header = %02x %02x %02x\n",
 
692
                   (full_failure == 48) ? "block": "sample",
 
693
                   inbuf [0], inbuf [1], inbuf [2]);
 
694
        }
 
695
      }
 
696
      if (full_failure == 48) {
 
697
        dv_audio -> block_failure++;
 
698
      }
 
699
    }
 
700
    dv_audio -> sample_failure += full_failure;
513
701
 
514
702
  } else {
515
703
    goto unsupported_sampling;
526
714
  fprintf(stderr, "libdv(%s):  not an audio block\n", __FUNCTION__);
527
715
  return(-1);
528
716
#endif
529
 
  
 
717
 
530
718
} /* dv_decode_audio_block */
 
719
 
 
720
/* ---------------------------------------------------------------------------
 
721
 */
 
722
void
 
723
dv_audio_correct_errors (dv_audio_t *dv_audio, int16_t **outbufs)
 
724
{
 
725
    int      num_ch, i, k, cnt;
 
726
    int16_t  *dptr, *sptr, last_valid, next_valid, diff;
 
727
 
 
728
  switch (dv_audio -> correction_method) {
 
729
  case DV_AUDIO_CORRECT_SILENCE:
 
730
    for (num_ch = 0; num_ch < dv_audio -> raw_num_channels; ++num_ch) {
 
731
      dptr = sptr = outbufs [num_ch];
 
732
      for (i = k = 0; i < dv_audio -> raw_samples_this_frame [num_ch / 2]; ++i) {
 
733
        if (*sptr == (int16_t) 0x8000) {
 
734
          ++k;
 
735
          ++sptr;
 
736
        } else {
 
737
          *dptr++ = *sptr++;
 
738
        }
 
739
      }
 
740
      if (k) {
 
741
        memset (dptr, 0, k);
 
742
      }
 
743
    }
 
744
    break;
 
745
  case DV_AUDIO_CORRECT_AVERAGE:
 
746
    for (num_ch = 0; num_ch < dv_audio -> raw_num_channels; ++num_ch) {
 
747
      dptr = sptr = outbufs [num_ch];
 
748
      last_valid = 0;
 
749
      for (i = 0; i < dv_audio -> raw_samples_this_frame [num_ch / 2]; i++) {
 
750
        if (*sptr != (int16_t) 0x8000) {
 
751
          last_valid = *dptr++ = *sptr++;
 
752
          continue;
 
753
        }
 
754
        for (k = i, cnt = 0;
 
755
             (k < dv_audio -> raw_samples_this_frame [num_ch / 2]) &&
 
756
             (*sptr == (int16_t) 0x8000);
 
757
             k++, cnt++, sptr++) {
 
758
          ;
 
759
        }
 
760
        i += (cnt - 1);
 
761
        next_valid = (k == dv_audio -> raw_samples_this_frame [num_ch / 2]) ? 0 : *sptr;
 
762
        diff = (next_valid - last_valid) / (cnt + 1);
 
763
#if 0
 
764
        fprintf (stderr,
 
765
                 " last_valid = 0x%04x diff =0x%04x next_valid = 0x%04x cnt = %d\n",
 
766
                 last_valid, diff, next_valid, cnt);
 
767
#endif
 
768
        while (cnt-- > 0) {
 
769
          last_valid += diff;
 
770
          *dptr++ = last_valid;
 
771
        }
 
772
      }
 
773
    }
 
774
    break;
 
775
  case DV_AUDIO_CORRECT_NONE:
 
776
    break;
 
777
  default:
 
778
    break;
 
779
  }
 
780
}
 
781
 
 
782
/* ---------------------------------------------------------------------------
 
783
 */
 
784
void
 
785
dv_audio_mix4ch (dv_audio_t *dv_audio, int16_t **outbufs)
 
786
{
 
787
    int     current_samples,
 
788
            num_ch,
 
789
            ch0_div, ch1_div,
 
790
            i, k;
 
791
    int16_t *dptr, *sptr;
 
792
 
 
793
  if (!(dv_audio -> raw_num_channels == 4))
 
794
    return;
 
795
 
 
796
  /* -------------------------------------------------------------------------
 
797
   * take entire channel 0
 
798
   */
 
799
  if (dv_audio -> arg_mixing_level >= 16)
 
800
    return;
 
801
 
 
802
  /* -------------------------------------------------------------------------
 
803
   * take entire channel 1
 
804
   */
 
805
  if (dv_audio -> arg_mixing_level <= -16)
 
806
  {
 
807
    for (num_ch = 0; num_ch < 2; ++num_ch)
 
808
    {
 
809
      dptr = outbufs [num_ch];
 
810
      sptr = outbufs [num_ch + 2];
 
811
      for (i = k = 0; i < dv_audio -> raw_samples_this_frame [1]; ++i)
 
812
      {
 
813
        *dptr++ = *sptr++;
 
814
      }
 
815
    }
 
816
    dv_audio -> raw_samples_this_frame [0] =
 
817
      dv_audio -> samples_this_frame =
 
818
      dv_audio -> raw_samples_this_frame [1];
 
819
    return;
 
820
  }
 
821
 
 
822
  /* -------------------------------------------------------------------------
 
823
   * mix both channles according to mixing level
 
824
   */
 
825
  current_samples = (dv_audio -> raw_samples_this_frame [0] >
 
826
                      dv_audio -> raw_samples_this_frame [1]) ?
 
827
                        dv_audio -> raw_samples_this_frame [1]:
 
828
                          dv_audio -> raw_samples_this_frame [0];
 
829
 
 
830
  ch0_div = ch1_div = 2;
 
831
 
 
832
  if (dv_audio -> arg_mixing_level < 0)
 
833
  {
 
834
    ch0_div = 1 << (1 - dv_audio -> arg_mixing_level);
 
835
  }
 
836
  else if (dv_audio -> arg_mixing_level > 0)
 
837
  {
 
838
    ch1_div = 1 << (1 + dv_audio -> arg_mixing_level);
 
839
  }
 
840
  for (num_ch = 0; num_ch < 2; ++num_ch)
 
841
  {
 
842
    dptr = outbufs [num_ch];
 
843
    sptr = outbufs [num_ch + 2];
 
844
    for (i = k = 0; i < current_samples; ++i)
 
845
    {
 
846
      *dptr = (*dptr / ch0_div) + (*sptr++ / ch1_div);
 
847
       dptr++;
 
848
    }
 
849
  }
 
850
  dv_audio -> raw_samples_this_frame [0] = dv_audio -> samples_this_frame = current_samples;
 
851
}
 
852
 
 
853
/* ---------------------------------------------------------------------------
 
854
 */
 
855
int
 
856
dv_set_audio_correction (dv_decoder_t *dv, int method)
 
857
{
 
858
    int  old_method;
 
859
 
 
860
  old_method = dv -> audio -> correction_method;
 
861
  dv -> audio -> correction_method = method;
 
862
  return old_method;
 
863
} /* dv_set_audio_correction */
 
864
 
 
865
/* ---------------------------------------------------------------------------
 
866
 */
 
867
int
 
868
dv_set_mixing_level (dv_decoder_t *dv, int new_value)
 
869
{
 
870
    int old_value;
 
871
 
 
872
  old_value = dv -> audio -> arg_mixing_level;
 
873
  dv -> audio -> arg_mixing_level = new_value;
 
874
  return (old_value);
 
875
}
 
876
 
 
877
/* ---------------------------------------------------------------------------
 
878
 */
 
879
int
 
880
dv_get_num_samples (dv_decoder_t *dv)
 
881
{
 
882
  return dv -> audio -> samples_this_frame;
 
883
}
 
884
 
 
885
/* ---------------------------------------------------------------------------
 
886
 */
 
887
int
 
888
dv_get_num_channels (dv_decoder_t *dv)
 
889
{
 
890
  return dv -> audio -> num_channels;
 
891
}
 
892
 
 
893
/* ---------------------------------------------------------------------------
 
894
 */
 
895
int
 
896
dv_is_4ch (dv_decoder_t *dv)
 
897
{
 
898
  return dv -> audio -> raw_num_channels == 4;
 
899
}
 
900
 
 
901
/* ---------------------------------------------------------------------------
 
902
 */
 
903
int
 
904
dv_get_raw_samples (dv_decoder_t *dv, int chan)
 
905
{
 
906
  return dv -> audio -> raw_samples_this_frame [chan];
 
907
}
 
908
 
 
909
/* ---------------------------------------------------------------------------
 
910
 */
 
911
int
 
912
dv_get_frequency (dv_decoder_t *dv)
 
913
{
 
914
  return dv -> audio -> frequency;
 
915
}
 
916
 
 
917
/* ---------------------------------------------------------------------------
 
918
 */
 
919
int
 
920
dv_is_new_recording (dv_decoder_t *dv, const uint8_t *buffer)
 
921
{
 
922
    int temp_time_stamp [4],
 
923
        zero_time_stamp [4] = {0, 0, 0, 0},
 
924
        new_recording = 0;
 
925
 
 
926
  /* -------------------------------------------------------------------------
 
927
   * we need valid and parsed audio headers for this
 
928
   */
 
929
  if (dv_parse_audio_header (dv, buffer))
 
930
  {
 
931
    /* -----------------------------------------------------------------------
 
932
     * only for 32kHz 12bit we need some extra checks
 
933
     */
 
934
    if (dv -> audio -> frequency == 32000 && dv -> audio -> quantization == 12)
 
935
    {
 
936
      /* ---------------------------------------------------------------------
 
937
       * 1. new recording by rec start in first channel
 
938
       */
 
939
      if (!dv -> audio -> aaux_asc. pc2. rec_st)
 
940
        new_recording++;
 
941
 
 
942
      /* ---------------------------------------------------------------------
 
943
       * reset frame change logic for rec end (frame changed twice)
 
944
       */
 
945
      dv_get_timestamp_int (dv, temp_time_stamp);
 
946
      if (!dv -> audio -> new_recording_on_next_frame &&
 
947
          memcmp (dv -> audio -> new_recording_current_time_stamp,
 
948
                  temp_time_stamp,
 
949
                  4 * sizeof (int)))
 
950
      {
 
951
        memcpy (dv -> audio -> new_recording_current_time_stamp,
 
952
                zero_time_stamp,
 
953
                4 * sizeof (int));
 
954
      }
 
955
 
 
956
      /* ---------------------------------------------------------------------
 
957
       * frame change flag is set and frame changed. so trigger new_recording.
 
958
       */
 
959
      if (dv -> audio -> new_recording_on_next_frame &&
 
960
          memcmp (dv -> audio -> new_recording_current_time_stamp,
 
961
                  temp_time_stamp,
 
962
                  4 * sizeof (int)))
 
963
      {
 
964
        dv -> audio -> new_recording_on_next_frame = 0;
 
965
      }
 
966
 
 
967
      /* ---------------------------------------------------------------------
 
968
       * 2. new recording by rec end some time ago and frame changed
 
969
       */
 
970
      if (memcmp (dv -> audio -> new_recording_current_time_stamp,
 
971
                   zero_time_stamp,
 
972
                   4 * sizeof (int)) &&
 
973
          !dv -> audio -> new_recording_on_next_frame)
 
974
      {
 
975
        new_recording++;
 
976
      }
 
977
 
 
978
      /* ---------------------------------------------------------------------
 
979
       * now set markers for next frame
 
980
       */
 
981
      if (dv -> audio -> raw_num_channels == 4 &&
 
982
          !dv -> audio -> aaux_asc1. pc2. rec_end)
 
983
      {
 
984
        memcpy (dv -> audio -> new_recording_current_time_stamp,
 
985
                temp_time_stamp,
 
986
                4 * sizeof (int));
 
987
        dv -> audio -> new_recording_on_next_frame = 1;
 
988
      }
 
989
    }
 
990
    else if (!dv -> audio -> aaux_asc. pc2. rec_st)
 
991
    {
 
992
        return new_recording = 1;
 
993
    }
 
994
 
 
995
  }
 
996
  return new_recording;
 
997
 
 
998
}
 
999
 
 
1000
#if 0
 
1001
/* ---------------------------------------------------------------------------
 
1002
 * dv_audio_do_fade returns:
 
1003
 *      0 for not fading action is required
 
1004
 *   <> 0 if fading action is required
 
1005
 *
 
1006
 *   *ch0, *ch1 are set to:
 
1007
 *      0 if no fading action is required for this channel
 
1008
 *      bit0 == 1 -> fade in should be done
 
1009
 *      bit1 == 1 -> fade out should be done
 
1010
 * function is TODO
 
1011
 * And what about a single frame with rec start and end and fade start/end set too ??
 
1012
 */
 
1013
int
 
1014
dv_audio_do_fade (dv_decoder_t *dv, int *ch0, int *ch1)
 
1015
{
 
1016
  *ch0 = *ch1 = 0;
 
1017
  return *ch0 + *ch1;
 
1018
}
 
1019
#endif
 
1020
 
 
1021
int dv_is_normal_speed (dv_decoder_t *dv)
 
1022
{
 
1023
  int normal_speed = TRUE;
 
1024
        
 
1025
  if (dv->std == e_dv_std_iec_61834) {
 
1026
    normal_speed = (dv->audio->aaux_asc.pc3.speed == 0x20);
 
1027
  } else if (dv->std == e_dv_std_smpte_314m) {
 
1028
    if(dv->audio->aaux_as.pc3.system) {
 
1029
      /* PAL */
 
1030
      normal_speed = (dv->audio->aaux_asc.pc3.speed == 0x64);
 
1031
    } else {
 
1032
      /* NTSC */
 
1033
      normal_speed = (dv->audio->aaux_asc.pc3.speed == 0x78);
 
1034
    } /* else */
 
1035
  }
 
1036
  return normal_speed;
 
1037
}