audio_out_aif.c 3.88 KB
Newer Older
1 2
/*
 * audio_out_aif.c
Michel Lespinasse's avatar
Michel Lespinasse committed
3
 * Copyright (C) 2000-2003 Michel Lespinasse <walken@zoy.org>
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
 * Copyright (C) 1999-2000 Aaron Holtzman <aholtzma@ess.engr.uvic.ca>
 *
 * This file is part of a52dec, a free ATSC A-52 stream decoder.
 * See http://liba52.sourceforge.net/ for updates.
 *
 * a52dec 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.
 *
 * a52dec 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <inttypes.h>

#include "a52.h"
#include "audio_out.h"
#include "audio_out_internal.h"

typedef struct aif_instance_s {
    ao_instance_t ao;
    int sample_rate;
    int set_params;
    int flags;
    int size;
} aif_instance_t;

static uint8_t aif_header[] = {
    'F', 'O', 'R', 'M', 0xff, 0xff, 0xff, 0xfe, 'A', 'I', 'F', 'F',
    'C', 'O', 'M', 'M', 0, 0, 0, 18,
46
    0, 2, 0x3f, 0xff, 0xff, 0xf4, 0, 16, 0x40, 0x0e, 0, 0, 0, 0, 0, 0, 0, 0,
Michel Lespinasse's avatar
Michel Lespinasse committed
47
    'S', 'S', 'N', 'D', 0xff, 0xff, 0xff, 0xd8, 0, 0, 0, 0, 0, 0, 0, 0
48 49
};

50
static int aif_setup (ao_instance_t * _instance, int sample_rate, int * flags,
51
		      level_t * level, sample_t * bias)
52 53 54 55 56 57 58 59
{
    aif_instance_t * instance = (aif_instance_t *) _instance;

    if ((instance->set_params == 0) && (instance->sample_rate != sample_rate))
	return 1;
    instance->sample_rate = sample_rate;

    *flags = instance->flags;
60 61
    *level = CONVERT_LEVEL;
    *bias = CONVERT_BIAS;
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

    return 0;
}

static void store4 (uint8_t * buf, int value)
{
    buf[0] = value >> 24;
    buf[1] = value >> 16;
    buf[2] = value >> 8;
    buf[3] = value;
}

static void store2 (uint8_t * buf, int16_t value)
{
    buf[0] = value >> 8;
    buf[1] = value;
}

80
static int aif_play (ao_instance_t * _instance, int flags, sample_t * _samples)
81 82 83 84 85
{
    aif_instance_t * instance = (aif_instance_t *) _instance;
    int16_t int16_samples[256*2];

#ifdef LIBA52_DOUBLE
86
    convert_t samples[256 * 2];
87 88 89 90 91
    int i;

    for (i = 0; i < 256 * 2; i++)
	samples[i] = _samples[i];
#else
92
    convert_t * samples = _samples;
93 94 95 96 97 98 99 100
#endif

    if (instance->set_params) {
	instance->set_params = 0;
	store2 (aif_header + 30, instance->sample_rate);
	fwrite (aif_header, sizeof (aif_header), 1, stdout);
    }

101
    convert2s16_2 (samples, int16_samples);
102 103 104 105 106 107 108 109
    s16_BE (int16_samples, 2);
    fwrite (int16_samples, 256 * sizeof (int16_t) * 2, 1, stdout);

    instance->size += 256 * sizeof (int16_t) * 2;

    return 0;
}

110
static void aif_close (ao_instance_t * _instance)
111 112 113 114 115 116
{
    aif_instance_t * instance = (aif_instance_t *) _instance;

    if (fseek (stdout, 0, SEEK_SET) < 0)
	return;

Michel Lespinasse's avatar
Michel Lespinasse committed
117
    store4 (aif_header + 4, instance->size + 46);
118
    store4 (aif_header + 22, instance->size / 4);
119
    store4 (aif_header + 42, instance->size + 8);
120 121 122
    fwrite (aif_header, sizeof (aif_header), 1, stdout);
}

123
static ao_instance_t * aif_open (int flags)
124 125 126
{
    aif_instance_t * instance;

127
    instance = (aif_instance_t *) malloc (sizeof (aif_instance_t));
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
    if (instance == NULL)
	return NULL;

    instance->ao.setup = aif_setup;
    instance->ao.play = aif_play;
    instance->ao.close = aif_close;

    instance->sample_rate = 0;
    instance->set_params = 1;
    instance->flags = flags;
    instance->size = 0;

    return (ao_instance_t *) instance;
}

ao_instance_t * ao_aif_open (void)
{
    return aif_open (A52_STEREO);
}

ao_instance_t * ao_aifdolby_open (void)
{
    return aif_open (A52_DOLBY);
}