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
|
/* encode.c
* - runtime encoding of PCM data.
*
* $Id: encode.c,v 1.19 2003/12/22 14:01:09 karl Exp $
*
* Copyright (c) 2001 Michael Smith <msmith@labyrinth.net.au>
*
* This program is distributed under the terms of the GNU General
* Public License, version 2. You may use, modify, and redistribute
* it under the terms of this license. A copy should be included
* with this source.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ogg/ogg.h>
#include <vorbis/codec.h>
#include <vorbis/vorbisenc.h>
#include "cfgparse.h"
#include "encode.h"
#define MODULE "encode/"
#include "logging.h"
static mutex_t _serial_lock;
static long _get_serial()
{
static long prev_serial = 0;
long serial;
thread_mutex_lock (&_serial_lock);
serial = prev_serial;
while (serial == prev_serial)
serial = rand();
prev_serial = serial;
thread_mutex_unlock (&_serial_lock);
return serial;
}
void encode_clear(encoder_state *s)
{
if(s)
{
LOG_DEBUG0("Clearing encoder engine");
ogg_stream_clear(&s->os);
vorbis_block_clear(&s->vb);
vorbis_dsp_clear(&s->vd);
vorbis_info_clear(&s->vi);
free(s);
}
}
encoder_state *encode_initialise(int channels, int rate, int managed,
int min_br, int nom_br, int max_br, float quality, vorbis_comment *vc)
{
encoder_state *s = calloc(1, sizeof(encoder_state));
ogg_packet h1,h2,h3;
/* Have vorbisenc choose a mode for us */
vorbis_info_init(&s->vi);
if (max_br < 0 && nom_br < 0 && min_br < 0)
managed = 0;
if (managed == 0 && nom_br >= 0)
if (min_br >= 0 || max_br >= 0)
managed = 1;
do
{
if (managed)
{
LOG_INFO5("Encoder initialising with bitrate management: %d "
"channels, %d Hz, minimum bitrate %d, nominal %d, "
"maximum %d", channels, rate, min_br, nom_br, max_br);
if (vorbis_encode_setup_managed (&s->vi, channels,
rate, max_br, nom_br, min_br))
break;
}
else
{
if (nom_br < 0)
{
LOG_INFO3("Encoder initialising in VBR mode: %d channel(s), "
"%d Hz, quality %f", channels, rate, quality);
if (min_br > 0 || max_br > 0)
LOG_INFO0("ignoring min/max bitrate, not supported in VBR "
"mode, use nominal-bitrate instead");
if (vorbis_encode_setup_vbr(&s->vi, channels, rate, quality*0.1))
break;
#if 0
if (max_br > 0 || min_br > 0)
{
struct ovectl_ratemanage_arg ai;
if (vorbis_encode_ctl(&s->vi, OV_ECTL_RATEMANAGE_GET, &ai))
break;
ai.bitrate_hard_min = min_br;
ai.bitrate_hard_max = max_br;
ai.management_active = 1;
if (vorbis_encode_ctl(&s->vi, OV_ECTL_RATEMANAGE_SET, &ai))
break;
}
#endif
}
else
{
LOG_INFO3("Encoder initialising in VBR mode: %d "
"channels, %d Hz, nominal %d", channels, rate, nom_br);
if (vorbis_encode_setup_managed (&s->vi, channels,
rate, max_br, nom_br, max_br))
break;
if (vorbis_encode_ctl (&s->vi, OV_ECTL_RATEMANAGE_SET, NULL))
break;
}
}
if (vorbis_encode_setup_init(&s->vi))
break;
vorbis_analysis_init(&s->vd, &s->vi);
vorbis_block_init(&s->vd, &s->vb);
ogg_stream_init(&s->os, _get_serial());
vorbis_analysis_headerout(&s->vd, vc, &h1,&h2,&h3);
ogg_stream_packetin(&s->os, &h1);
ogg_stream_packetin(&s->os, &h2);
ogg_stream_packetin(&s->os, &h3);
s->in_header = 1;
s->samplerate = rate;
s->samples_in_current_page = 0;
s->prevgranulepos = 0;
s->max_samples_ppage = rate*2;
return s;
} while (0);
LOG_INFO0("Failed to configure encoder, verify settings");
vorbis_info_clear(&s->vi);
free (s);
return NULL;
}
void encode_data_float(encoder_state *s, float **pcm, int samples)
{
float **buf;
int i;
buf = vorbis_analysis_buffer(&s->vd, samples);
for(i=0; i < s->vi.channels; i++)
{
memcpy(buf[i], pcm[i], samples*sizeof(float));
}
vorbis_analysis_wrote(&s->vd, samples);
s->samples_in_current_page += samples;
}
/* Requires little endian data (currently) */
void encode_data(encoder_state *s, signed char *buf, int bytes, int bigendian)
{
float **buffer;
int i,j;
int channels = s->vi.channels;
int samples = bytes/(2*channels);
buffer = vorbis_analysis_buffer(&s->vd, samples);
if(bigendian)
{
for(i=0; i < samples; i++)
{
for(j=0; j < channels; j++)
{
buffer[j][i]=((buf[2*(i*channels + j)]<<8) |
(0x00ff&(int)buf[2*(i*channels + j)+1]))/32768.f;
}
}
}
else
{
for(i=0; i < samples; i++)
{
for(j=0; j < channels; j++)
{
buffer[j][i]=((buf[2*(i*channels + j) + 1]<<8) |
(0x00ff&(int)buf[2*(i*channels + j)]))/32768.f;
}
}
}
vorbis_analysis_wrote(&s->vd, samples);
s->samples_in_current_page += samples;
}
/* Returns:
* 0 No output at this time
* >0 Page produced
*
* Caller should loop over this to ensure that we don't end up with
* excessive buffering in libvorbis.
*/
int encode_dataout(encoder_state *s, ogg_page *og)
{
ogg_packet op;
int result;
if(s->in_header)
{
result = ogg_stream_flush(&s->os, og);
if(result==0)
{
s->in_header = 0;
return encode_dataout(s,og);
}
else
return 1;
}
else
{
while(vorbis_analysis_blockout(&s->vd, &s->vb)==1)
{
vorbis_analysis(&s->vb, NULL);
vorbis_bitrate_addblock(&s->vb);
while(vorbis_bitrate_flushpacket(&s->vd, &op))
ogg_stream_packetin(&s->os, &op);
}
/* FIXME: Make this threshold configurable.
* We don't want to buffer too many samples in one page when doing
* live encoding - that's fine for non-live encoding, but breaks
* badly when doing things live.
* So, we flush the stream if we have too many samples buffered
*/
if(s->samples_in_current_page > s->max_samples_ppage)
{
/*LOG_DEBUG1("Forcing flush: Too many samples in current page (%d)",
s->samples_in_current_page); */
result = ogg_stream_flush(&s->os, og);
}
else
result = ogg_stream_pageout(&s->os, og);
if(result==0)
return 0;
else /* Page found! */
{
s->samples_in_current_page -= ogg_page_granulepos(og) -
s->prevgranulepos;
s->prevgranulepos = ogg_page_granulepos(og);
return 1;
}
}
}
void encode_finish(encoder_state *s)
{
ogg_packet op;
vorbis_analysis_wrote(&s->vd, 0);
while(vorbis_analysis_blockout(&s->vd, &s->vb)==1)
{
vorbis_analysis(&s->vb, NULL);
vorbis_bitrate_addblock(&s->vb);
while(vorbis_bitrate_flushpacket(&s->vd, &op))
ogg_stream_packetin(&s->os, &op);
}
}
int encode_flush(encoder_state *s, ogg_page *og)
{
int result = ogg_stream_pageout(&s->os, og);
if(result<=0)
return 0;
else
return 1;
}
void encode_init()
{
thread_mutex_create (&_serial_lock);
}
void encode_close()
{
thread_mutex_destroy (&_serial_lock);
}
|