00001
00002 #include <cstdio>
00003 #include <unistd.h>
00004 #include <string.h>
00005
00006 #include "config.h"
00007
00008
00009 extern "C" {
00010 #include "libavcodec/avcodec.h"
00011 #ifdef ENABLE_AC3_DECODER
00012 #include "libavcodec/parser.h"
00013 #else
00014 #include <a52dec/a52.h>
00015 #endif
00016 }
00017
00018
00019 #include "mythcontext.h"
00020 #include "audiooutputdigitalencoder.h"
00021 #include "compat.h"
00022
00023 #define LOC QString("DEnc: ")
00024 #define LOC_ERR QString("DEnc, Error: ")
00025
00026 #define MAX_AC3_FRAME_SIZE 6144
00027
00028 AudioOutputDigitalEncoder::AudioOutputDigitalEncoder(void) :
00029 av_context(NULL),
00030 outbuf(NULL),
00031 outbuf_size(0),
00032 frame_buffer(NULL),
00033 one_frame_bytes(0)
00034 {
00035 }
00036
00037 AudioOutputDigitalEncoder::~AudioOutputDigitalEncoder()
00038 {
00039 Dispose();
00040 }
00041
00042 void AudioOutputDigitalEncoder::Dispose()
00043 {
00044 if (av_context)
00045 {
00046 avcodec_close(av_context);
00047 av_free(av_context);
00048 av_context = NULL;
00049 }
00050
00051 if (outbuf)
00052 {
00053 delete [] outbuf;
00054 outbuf = NULL;
00055 outbuf_size = 0;
00056 }
00057
00058 if (frame_buffer)
00059 {
00060 delete [] frame_buffer;
00061 frame_buffer = NULL;
00062 one_frame_bytes = 0;
00063 }
00064 }
00065
00066
00067 bool AudioOutputDigitalEncoder::Init(
00068 CodecID codec_id, int bitrate, int samplerate, int channels)
00069 {
00070 AVCodec *codec;
00071 int ret;
00072
00073 VERBOSE(VB_AUDIO, LOC + QString("Init codecid=%1, br=%2, sr=%3, ch=%4")
00074 .arg(codec_id_string(codec_id))
00075 .arg(bitrate)
00076 .arg(samplerate)
00077 .arg(channels));
00078
00079
00080
00081 codec = avcodec_find_encoder(CODEC_ID_AC3);
00082 if (!codec)
00083 {
00084 VERBOSE(VB_IMPORTANT, LOC_ERR + "Could not find codec");
00085 return false;
00086 }
00087
00088 av_context = avcodec_alloc_context();
00089 av_context->bit_rate = bitrate;
00090 av_context->sample_rate = samplerate;
00091 av_context->channels = channels;
00092
00093
00094 ret = avcodec_open(av_context, codec);
00095 if (ret < 0)
00096 {
00097 VERBOSE(VB_IMPORTANT, LOC_ERR +
00098 "Could not open codec, invalid bitrate or samplerate");
00099
00100 Dispose();
00101 return false;
00102 }
00103
00104 size_t bytes_per_frame = av_context->channels * sizeof(short);
00105 audio_bytes_per_sample = bytes_per_frame;
00106 one_frame_bytes = bytes_per_frame * av_context->frame_size;
00107
00108 outbuf_size = 16384;
00109 outbuf = new char [outbuf_size];
00110 VERBOSE(VB_AUDIO, QString("DigitalEncoder::Init fs=%1, bpf=%2 ofb=%3")
00111 .arg(av_context->frame_size)
00112 .arg(bytes_per_frame)
00113 .arg(one_frame_bytes));
00114
00115 return true;
00116 }
00117
00118 static int DTS_SAMPLEFREQS[16] =
00119 {
00120 0, 8000, 16000, 32000, 64000, 128000, 11025, 22050,
00121 44100, 88200, 176400, 12000, 24000, 48000, 96000, 192000
00122 };
00123
00124 static int DTS_BITRATES[30] =
00125 {
00126 32000, 56000, 64000, 96000, 112000, 128000,
00127 192000, 224000, 256000, 320000, 384000, 448000,
00128 512000, 576000, 640000, 768000, 896000, 1024000,
00129 1152000, 1280000, 1344000, 1408000, 1411200, 1472000,
00130 1536000, 1920000, 2048000, 3072000, 3840000, 4096000
00131 };
00132
00133 static int dts_decode_header(uint8_t *indata_ptr, int *rate,
00134 int *nblks, int *sfreq)
00135 {
00136 uint id = ((indata_ptr[0] << 24) | (indata_ptr[1] << 16) |
00137 (indata_ptr[2] << 8) | (indata_ptr[3]));
00138
00139 if (id != 0x7ffe8001)
00140 return -1;
00141
00142 int ftype = indata_ptr[4] >> 7;
00143
00144 int surp = (indata_ptr[4] >> 2) & 0x1f;
00145 surp = (surp + 1) % 32;
00146
00147 *nblks = (indata_ptr[4] & 0x01) << 6 | (indata_ptr[5] >> 2);
00148 ++*nblks;
00149
00150 int fsize = (indata_ptr[5] & 0x03) << 12 |
00151 (indata_ptr[6] << 4) | (indata_ptr[7] >> 4);
00152 ++fsize;
00153
00154 *sfreq = (indata_ptr[8] >> 2) & 0x0f;
00155 *rate = (indata_ptr[8] & 0x03) << 3 | ((indata_ptr[9] >> 5) & 0x07);
00156
00157 if (ftype != 1)
00158 {
00159 VERBOSE(VB_IMPORTANT, LOC +
00160 QString("DTS: Termination frames not handled (ftype %1)")
00161 .arg(ftype));
00162 return -1;
00163 }
00164
00165 if (*sfreq != 13)
00166 {
00167 VERBOSE(VB_IMPORTANT, LOC +
00168 QString("DTS: Only 48kHz supported (sfreq %1)").arg(*sfreq));
00169 return -1;
00170 }
00171
00172 if ((fsize > 8192) || (fsize < 96))
00173 {
00174 VERBOSE(VB_IMPORTANT, LOC +
00175 QString("DTS: fsize: %1 invalid").arg(fsize));
00176 return -1;
00177 }
00178
00179 if (*nblks != 8 && *nblks != 16 && *nblks != 32 &&
00180 *nblks != 64 && *nblks != 128 && ftype == 1)
00181 {
00182 VERBOSE(VB_IMPORTANT, LOC +
00183 QString("DTS: nblks %1 not valid for normal frame")
00184 .arg(*nblks));
00185 return -1;
00186 }
00187
00188 return fsize;
00189 }
00190
00191 static int dts_syncinfo(uint8_t *indata_ptr, int *flags,
00192 int *sample_rate, int *bit_rate)
00193 {
00194 (void) flags;
00195
00196 int nblks;
00197 int rate;
00198 int sfreq;
00199
00200 int fsize = dts_decode_header(indata_ptr, &rate, &nblks, &sfreq);
00201 if (fsize >= 0)
00202 {
00203 *bit_rate = (rate >= 0 && rate <= 29) ? DTS_BITRATES[rate] : 0;
00204 *sample_rate = (sfreq >= 1 && sfreq <= 15)? DTS_SAMPLEFREQS[sfreq] : 0;
00205 }
00206
00207 return fsize;
00208 }
00209
00210
00211
00212 extern "C" int ac3_sync(const uint8_t *buf, int *channels, int *sample_rate,
00213 int *bit_rate, int *samples);
00214
00215
00216
00217 typedef struct {
00218
00219 unsigned professional_consumer:1;
00220 unsigned non_data:1;
00221
00222
00223
00224 unsigned audio_signal_emphasis:3;
00225 unsigned SSFL:1;
00226
00227
00228
00229
00230 unsigned sample_frequency:2;
00231
00232
00233
00234
00235
00236
00237 unsigned channel_mode:4;
00238
00239
00240
00241
00242 unsigned user_bit_management:4;
00243
00244
00245
00246 unsigned auxiliary_bits:3;
00247
00248
00249 unsigned source_word_length:3;
00250 unsigned reserved:2;
00251
00252 unsigned multi_channel_function_description:8;
00253
00254 unsigned digital_audio_reference_signal:2;
00255 unsigned reserved2:6;
00256
00257 } AESHeader;
00258
00259 static int encode_frame(
00260 bool dts,
00261 unsigned char *data,
00262 size_t &len)
00263 {
00264 size_t enc_len;
00265 int flags, sample_rate, bit_rate;
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275 uint nr_samples = 0, block_len;
00276 if (dts)
00277 {
00278 enc_len = dts_syncinfo(data+8, &flags, &sample_rate, &bit_rate);
00279 int rate, sfreq, nblks;
00280 dts_decode_header(data+8, &rate, &nblks, &sfreq);
00281 nr_samples = nblks * 32;
00282 block_len = nr_samples * 2 * 2;
00283 }
00284 else
00285 {
00286 #ifdef ENABLE_AC3_DECODER
00287 enc_len = ac3_sync(
00288 data + 8, &flags, &sample_rate, &bit_rate, (int*)&block_len);
00289 block_len *= 2 * 2;
00290 #else
00291 enc_len = a52_syncinfo(data + 8, &flags, &sample_rate, &bit_rate);
00292 block_len = MAX_AC3_FRAME_SIZE;
00293 #endif
00294 }
00295
00296 if (enc_len == 0 || enc_len > len)
00297 {
00298 int l = len;
00299 len = 0;
00300 return l;
00301 }
00302
00303 enc_len = min((uint)enc_len, block_len - 8);
00304
00305
00306
00307 swab((const char *)data + 8, (char *)data + 8, enc_len);
00308
00309
00310
00311
00312
00313
00314 data[0] = 0x72;
00315 data[1] = 0xF8;
00316 data[2] = 0x1F;
00317 data[3] = 0x4E;
00318 data[4] = 0x01;
00319 if (dts)
00320 {
00321 switch(nr_samples)
00322 {
00323 case 256:
00324 data[4] = 0x0A;
00325 break;
00326
00327 case 512:
00328 data[4] = 0x0B;
00329 break;
00330
00331 case 1024:
00332 data[4] = 0x0C;
00333 break;
00334
00335 case 2048:
00336 data[4] = 0x0D;
00337 break;
00338
00339 case 4096:
00340 data[4] = 0x0E;
00341 break;
00342
00343 default:
00344 VERBOSE(VB_IMPORTANT, LOC +
00345 QString("DTS: %1-sample bursts not supported")
00346 .arg(nr_samples));
00347 data[4] = 0x00;
00348 break;
00349 }
00350 }
00351 data[5] = 0x00;
00352 data[6] = (enc_len << 3) & 0xFF;
00353 data[7] = (enc_len >> 5) & 0xFF;
00354 memset(data + 8 + enc_len, 0, block_len - 8 - enc_len);
00355 len = block_len;
00356
00357 return enc_len;
00358 }
00359
00360
00361 size_t AudioOutputDigitalEncoder::Encode(short *buff)
00362 {
00363 int encsize = 0;
00364 size_t outsize = 0;
00365
00366
00367 outsize = avcodec_encode_audio(
00368 av_context, ((uchar*)outbuf) + 8, outbuf_size - 8, buff);
00369
00370 size_t tmpsize = outsize;
00371
00372 outsize = MAX_AC3_FRAME_SIZE;
00373 encsize = encode_frame(
00374 false,
00375 (unsigned char*)outbuf, outsize);
00376
00377 VERBOSE(VB_AUDIO|VB_TIMESTAMP,
00378 QString("DigitalEncoder::Encode len1=%1 len2=%2 finallen=%3")
00379 .arg(tmpsize).arg(encsize).arg(outsize));
00380
00381 return outsize;
00382 }