summaryrefslogtreecommitdiff
path: root/src/liblpcm/xine_decoder.c
blob: 89a34fc35d08d2dd3a7dcd8be3ac636d74f91eaa (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
/* 
 * Copyright (C) 2000-2001 the xine project
 * 
 * This file is part of xine, a unix video player.
 * 
 * xine 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.
 * 
 * xine 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
 *
 * $Id: xine_decoder.c,v 1.7 2001/09/06 13:36:18 jkeil Exp $
 * 
 * 31-8-2001 Added LPCM rate sensing.
 *   (c) 2001 James Courtier-Dutton James@superbug.demon.co.uk
 *
 * stuff needed to turn libac3 into a xine decoder plugin
 */


#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <netinet/in.h> /* ntohs */
#include "audio_out.h"
#include "buffer.h"
#include "xine_internal.h"


typedef struct lpcm_decoder_s {
  audio_decoder_t  audio_decoder;

  uint32_t         pts;
  uint32_t         rate;
  uint32_t         bits_per_sample; 
  uint32_t         number_of_channels; 
  uint32_t         ao_cap_mode; 
   
  ao_instance_t  *audio_out;
  int              output_open;
  int		   cpu_be;	/* TRUE, if we're a Big endian CPU */
} lpcm_decoder_t;

int lpcm_can_handle (audio_decoder_t *this_gen, int buf_type) {
  buf_type &= 0xFFFF0000;

  return ( buf_type == BUF_AUDIO_LPCM_BE ||
	   buf_type == BUF_AUDIO_LPCM_LE );
}


void lpcm_init (audio_decoder_t *this_gen, ao_instance_t *audio_out) {

  lpcm_decoder_t *this = (lpcm_decoder_t *) this_gen;

  this->audio_out     = audio_out;
  this->output_open   = 0;
  this->pts           = 0;
  this->rate          = 0;
  this->bits_per_sample=0; 
  this->number_of_channels=0; 
  this->ao_cap_mode=0; 
 
  this->cpu_be        = ( htons(1) == 1 );
}


void lpcm_decode_data (audio_decoder_t *this_gen, buf_element_t *buf) {

  lpcm_decoder_t *this = (lpcm_decoder_t *) this_gen;
  int16_t *sample_buffer=(int16_t *)buf->content;
  int stream_be;

  this->pts = buf->PTS;
  this->rate=buf->decoder_info[1];
  this->bits_per_sample=buf->decoder_info[2] ; 
  this->number_of_channels=buf->decoder_info[3] ; 
  this->ao_cap_mode=(this->number_of_channels == 2) ? AO_CAP_MODE_STEREO : AO_CAP_MODE_MONO; 
  if (buf->decoder_info[0] == 0)
    return;
  if (!this->output_open) {      
    this->output_open = (this->audio_out->open (this->audio_out, this->bits_per_sample, 
						this->rate,
						this->ao_cap_mode) == 1);
  }
  if (!this->output_open) 
    return;

  /* Swap LPCM samples into native byte order, if necessary */
  stream_be = ( buf->type == BUF_AUDIO_LPCM_BE );
  if (stream_be != this->cpu_be)
    swab(sample_buffer, sample_buffer, buf->size);

  this->audio_out->write (this->audio_out,
                            sample_buffer,
                            (((buf->size*8)/this->number_of_channels)/this->bits_per_sample),
                            this->pts);

  
}

void lpcm_close (audio_decoder_t *this_gen) {

  lpcm_decoder_t *this = (lpcm_decoder_t *) this_gen; 

  if (this->output_open) 
    this->audio_out->close (this->audio_out);
  this->output_open = 0;
}

static char *lpcm_get_id(void) {
  return "lpcm";
}

audio_decoder_t *init_audio_decoder_plugin (int iface_version, config_values_t *cfg) {

  lpcm_decoder_t *this ;

  if (iface_version != 2) {
    printf( "liblpcm: plugin doesn't support plugin API version %d.\n"
	    "liblpcm: this means there's a version mismatch between xine and this "
	    "liblpcm: decoder plugin.\nInstalling current plugins should help.\n",
	    iface_version);
    return NULL;
  }

  this = (lpcm_decoder_t *) malloc (sizeof (lpcm_decoder_t));

  this->audio_decoder.interface_version   = 2;
  this->audio_decoder.can_handle          = lpcm_can_handle;
  this->audio_decoder.init                = lpcm_init;
  this->audio_decoder.decode_data         = lpcm_decode_data;
  this->audio_decoder.close               = lpcm_close;
  this->audio_decoder.get_identifier      = lpcm_get_id;
  this->audio_decoder.priority            = 1;
    
  return (audio_decoder_t *) this;
}