source: roaraudio/libroardsp/transcode.c @ 2176:c8fe291eaa53

Last change on this file since 2176:c8fe291eaa53 was 2176:c8fe291eaa53, checked in by phi, 15 years ago

wrote basic codec

File size: 4.4 KB
Line 
1//transcode.c:
2
3/*
4 *      Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009
5 *
6 *  This file is part of libroardsp a part of RoarAudio,
7 *  a cross-platform sound system for both, home and professional use.
8 *  See README for details.
9 *
10 *  This file is free software; you can redistribute it and/or modify
11 *  it under the terms of the GNU General Public License version 3
12 *  as published by the Free Software Foundation.
13 *
14 *  libroardsp is distributed in the hope that it will be useful,
15 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
16 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 *  GNU General Public License for more details.
18 *
19 *  You should have received a copy of the GNU General Public License
20 *  along with this software; see the file COPYING.  If not, write to
21 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 */
24
25#include "libroardsp.h"
26
27#define _FUNC(func) (state->entry->func)
28#define _CHECK_STATE() (!(state == NULL || state->entry == NULL))
29#define _CHECK_FUNC(func) (_CHECK_STATE() && _FUNC(func) != NULL)
30#define _CHECK_BASIC(func) if ( !_CHECK_FUNC(func) ) return -1
31#define _CHECK() if ( !_CHECK_STATE() ) return -1
32
33static struct roar_xcoder_entry g_xcoders[] = {
34 {ROAR_CODEC_ALAW,  roar_xcoder_dummy_inituninit, roar_xcoder_dummy_inituninit, roar_xcoder_dummy_packet_size_any,
35                     NULL, NULL},
36 {ROAR_CODEC_MULAW, roar_xcoder_dummy_inituninit, roar_xcoder_dummy_inituninit, roar_xcoder_dummy_packet_size_any,
37                     NULL, NULL},
38 {-1, NULL, NULL, NULL, NULL, NULL}
39};
40
41int roar_xcoder_init(struct roar_xcoder * state, int encoder, struct roar_audio_info * info, struct roar_vio_calls * vio) {
42 int i;
43
44 if ( state == NULL || info == NULL )
45  return -1;
46
47 memset(state, 0, sizeof(struct roar_xcoder));
48
49 for (i = 0; g_xcoders[i].codec != -1; i++) {
50  if ( g_xcoders[i].codec == info->codec ) {
51   state->entry = &(g_xcoders[i]);
52   break;
53  }
54 }
55
56 if ( state->entry == NULL )
57  return -1;
58
59 state->stage      = ROAR_XCODER_STAGE_NONE;
60 state->encode     = encoder;
61 state->packet_len = -1;
62
63 if ( roar_xcoder_set_backend(state, vio) == -1 )
64  return -1;
65
66 memcpy(&(state->info.coded), info, sizeof(struct roar_audio_info));
67 memcpy(&(state->info.pcm  ), info, sizeof(struct roar_audio_info));
68
69 state->info.pcm.codec = ROAR_CODEC_DEFAULT;
70
71 if ( _FUNC(init) == NULL )
72  return -1;
73
74 if ( _FUNC(init)(state) != 0 )
75  return -1;
76
77 state->stage      = ROAR_XCODER_STAGE_INITED;
78
79 return 0;
80}
81
82int roar_xcoder_set_backend(struct roar_xcoder * state, struct roar_vio_calls * vio) {
83 _CHECK();
84
85 if ( vio == NULL && state->backend != NULL )
86  return -1;
87
88 state->backend = vio;
89
90 return 0;
91}
92int roar_xcoder_packet_size(struct roar_xcoder * state, int samples) {
93 _CHECK_BASIC(packet_size);
94
95 state->packet_len = state->entry->packet_size(state, samples);
96
97 if ( state->packet_len == 0 ) {
98  if ( samples < 1 ) {
99   return ROAR_RATE_DEFAULT/100;
100  } else {
101   return samples;
102  }
103 }
104
105 return state->packet_len;
106}
107
108int roar_xcoder_close      (struct roar_xcoder * state) {
109 _CHECK_BASIC(uninit);
110
111
112 if ( state->iobuffer != NULL ) {
113  roar_xcoder_proc(state, NULL, 0); // try to flush
114  roar_buffer_free(state->iobuffer);
115 }
116
117 return _FUNC(uninit)(state);
118}
119
120int roar_xcoder_proc_packet(struct roar_xcoder * state, void * buf, size_t len) {
121 _CHECK();
122
123 if ( state->encode ) {
124  _CHECK_BASIC(encode);
125  return _FUNC(encode)(state, buf, len);
126 } else {
127  _CHECK_BASIC(decode);
128  return _FUNC(decode)(state, buf, len);
129 }
130}
131
132int roar_xcoder_proc       (struct roar_xcoder * state, void * buf, size_t len) {
133 return -1;
134}
135
136int roar_bixcoder_init(struct roar_bixcoder * state, struct roar_audio_info * info, struct roar_vio_calls * vio);
137int roar_bixcoder_packet_size (struct roar_bixcoder * state, int samples);
138int roar_bixcoder_close       (struct roar_bixcoder * state);
139int roar_bixcoder_read_packet (struct roar_bixcoder * state, void * buf, size_t len);
140int roar_bixcoder_read        (struct roar_bixcoder * state, void * buf, size_t len);
141int roar_bixcoder_write_packet(struct roar_bixcoder * state, void * buf, size_t len);
142int roar_bixcoder_write       (struct roar_bixcoder * state, void * buf, size_t len);
143
144// dummy functions used by some de/encoders:
145int roar_xcoder_dummy_inituninit(struct roar_xcoder * state) {
146 return 0;
147}
148
149int roar_xcoder_dummy_packet_size_any(struct roar_xcoder * state, int samples) {
150 // the case samples=-1/samples!=-1 based things are done in the general func
151 return 0;
152}
153
154//ll
Note: See TracBrowser for help on using the repository browser.