00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00034 #include <limits.h>
00035
00036 #include "avcodec.h"
00037 #include "bitstream.h"
00038 #include "golomb.h"
00039
00040 #undef NDEBUG
00041 #include <assert.h>
00042
00043 #define MAX_CHANNELS 8
00044 #define MAX_BLOCKSIZE 65535
00045 #define FLAC_STREAMINFO_SIZE 34
00046
00047 enum decorrelation_type {
00048 INDEPENDENT,
00049 LEFT_SIDE,
00050 RIGHT_SIDE,
00051 MID_SIDE,
00052 };
00053
00054 typedef struct FLACContext {
00055 AVCodecContext *avctx;
00056 GetBitContext gb;
00057
00058 int min_blocksize, max_blocksize;
00059 int min_framesize, max_framesize;
00060 int samplerate, channels;
00061 int blocksize;
00062 int bps, curr_bps;
00063 enum decorrelation_type decorrelation;
00064
00065 int32_t *decoded[MAX_CHANNELS];
00066 uint8_t *bitstream;
00067 int bitstream_size;
00068 int bitstream_index;
00069 int allocated_bitstream_size;
00070 } FLACContext;
00071
00072 #define METADATA_TYPE_STREAMINFO 0
00073
00074 static int sample_rate_table[] =
00075 { 0, 0, 0, 0,
00076 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000,
00077 0, 0, 0, 0 };
00078
00079 static int sample_size_table[] =
00080 { 0, 8, 12, 0, 16, 20, 24, 0 };
00081
00082 static int blocksize_table[] = {
00083 0, 192, 576<<0, 576<<1, 576<<2, 576<<3, 0, 0,
00084 256<<0, 256<<1, 256<<2, 256<<3, 256<<4, 256<<5, 256<<6, 256<<7
00085 };
00086
00087 static const uint8_t table_crc8[256] = {
00088 0x00, 0x07, 0x0e, 0x09, 0x1c, 0x1b, 0x12, 0x15,
00089 0x38, 0x3f, 0x36, 0x31, 0x24, 0x23, 0x2a, 0x2d,
00090 0x70, 0x77, 0x7e, 0x79, 0x6c, 0x6b, 0x62, 0x65,
00091 0x48, 0x4f, 0x46, 0x41, 0x54, 0x53, 0x5a, 0x5d,
00092 0xe0, 0xe7, 0xee, 0xe9, 0xfc, 0xfb, 0xf2, 0xf5,
00093 0xd8, 0xdf, 0xd6, 0xd1, 0xc4, 0xc3, 0xca, 0xcd,
00094 0x90, 0x97, 0x9e, 0x99, 0x8c, 0x8b, 0x82, 0x85,
00095 0xa8, 0xaf, 0xa6, 0xa1, 0xb4, 0xb3, 0xba, 0xbd,
00096 0xc7, 0xc0, 0xc9, 0xce, 0xdb, 0xdc, 0xd5, 0xd2,
00097 0xff, 0xf8, 0xf1, 0xf6, 0xe3, 0xe4, 0xed, 0xea,
00098 0xb7, 0xb0, 0xb9, 0xbe, 0xab, 0xac, 0xa5, 0xa2,
00099 0x8f, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9d, 0x9a,
00100 0x27, 0x20, 0x29, 0x2e, 0x3b, 0x3c, 0x35, 0x32,
00101 0x1f, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0d, 0x0a,
00102 0x57, 0x50, 0x59, 0x5e, 0x4b, 0x4c, 0x45, 0x42,
00103 0x6f, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7d, 0x7a,
00104 0x89, 0x8e, 0x87, 0x80, 0x95, 0x92, 0x9b, 0x9c,
00105 0xb1, 0xb6, 0xbf, 0xb8, 0xad, 0xaa, 0xa3, 0xa4,
00106 0xf9, 0xfe, 0xf7, 0xf0, 0xe5, 0xe2, 0xeb, 0xec,
00107 0xc1, 0xc6, 0xcf, 0xc8, 0xdd, 0xda, 0xd3, 0xd4,
00108 0x69, 0x6e, 0x67, 0x60, 0x75, 0x72, 0x7b, 0x7c,
00109 0x51, 0x56, 0x5f, 0x58, 0x4d, 0x4a, 0x43, 0x44,
00110 0x19, 0x1e, 0x17, 0x10, 0x05, 0x02, 0x0b, 0x0c,
00111 0x21, 0x26, 0x2f, 0x28, 0x3d, 0x3a, 0x33, 0x34,
00112 0x4e, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5c, 0x5b,
00113 0x76, 0x71, 0x78, 0x7f, 0x6a, 0x6d, 0x64, 0x63,
00114 0x3e, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2c, 0x2b,
00115 0x06, 0x01, 0x08, 0x0f, 0x1a, 0x1d, 0x14, 0x13,
00116 0xae, 0xa9, 0xa0, 0xa7, 0xb2, 0xb5, 0xbc, 0xbb,
00117 0x96, 0x91, 0x98, 0x9f, 0x8a, 0x8d, 0x84, 0x83,
00118 0xde, 0xd9, 0xd0, 0xd7, 0xc2, 0xc5, 0xcc, 0xcb,
00119 0xe6, 0xe1, 0xe8, 0xef, 0xfa, 0xfd, 0xf4, 0xf3
00120 };
00121
00122 static int64_t get_utf8(GetBitContext *gb)
00123 {
00124 uint64_t val;
00125 int ones=0, bytes;
00126
00127 while(get_bits1(gb))
00128 ones++;
00129
00130 if (ones==0) bytes=0;
00131 else if(ones==1) return -1;
00132 else bytes= ones - 1;
00133
00134 val= get_bits(gb, 7-ones);
00135 while(bytes--){
00136 const int tmp = get_bits(gb, 8);
00137
00138 if((tmp>>6) != 2)
00139 return -1;
00140 val<<=6;
00141 val|= tmp&0x3F;
00142 }
00143 return val;
00144 }
00145
00146 #if 0
00147 static int skip_utf8(GetBitContext *gb)
00148 {
00149 int ones=0, bytes;
00150
00151 while(get_bits1(gb))
00152 ones++;
00153
00154 if (ones==0) bytes=0;
00155 else if(ones==1) return -1;
00156 else bytes= ones - 1;
00157
00158 skip_bits(gb, 7-ones);
00159 while(bytes--){
00160 const int tmp = get_bits(gb, 8);
00161
00162 if((tmp>>6) != 2)
00163 return -1;
00164 }
00165 return 0;
00166 }
00167 #endif
00168
00169 static int get_crc8(const uint8_t *buf, int count){
00170 int crc=0;
00171 int i;
00172
00173 for(i=0; i<count; i++){
00174 crc = table_crc8[crc ^ buf[i]];
00175 }
00176
00177 return crc;
00178 }
00179
00180 static void metadata_streaminfo(FLACContext *s);
00181 static void dump_headers(FLACContext *s);
00182
00183 static int flac_decode_init(AVCodecContext * avctx)
00184 {
00185 FLACContext *s = avctx->priv_data;
00186 s->avctx = avctx;
00187
00188
00189 if (avctx->extradata_size == FLAC_STREAMINFO_SIZE) {
00190 init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size*8);
00191 metadata_streaminfo(s);
00192 dump_headers(s);
00193 }
00194
00195 return 0;
00196 }
00197
00198 static void dump_headers(FLACContext *s)
00199 {
00200 av_log(s->avctx, AV_LOG_DEBUG, " Blocksize: %d .. %d (%d)\n", s->min_blocksize, s->max_blocksize, s->blocksize);
00201 av_log(s->avctx, AV_LOG_DEBUG, " Framesize: %d .. %d\n", s->min_framesize, s->max_framesize);
00202 av_log(s->avctx, AV_LOG_DEBUG, " Samplerate: %d\n", s->samplerate);
00203 av_log(s->avctx, AV_LOG_DEBUG, " Channels: %d\n", s->channels);
00204 av_log(s->avctx, AV_LOG_DEBUG, " Bits: %d\n", s->bps);
00205 }
00206
00207 static void allocate_buffers(FLACContext *s){
00208 int i;
00209
00210 assert(s->max_blocksize);
00211
00212 if(s->max_framesize == 0 && s->max_blocksize){
00213 s->max_framesize= (s->channels * s->bps * s->max_blocksize + 7)/ 8;
00214 }
00215
00216 for (i = 0; i < s->channels; i++)
00217 {
00218 s->decoded[i] = av_realloc(s->decoded[i], sizeof(int32_t)*s->max_blocksize);
00219 }
00220
00221 s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);
00222 }
00223
00224 static void metadata_streaminfo(FLACContext *s)
00225 {
00226
00227 s->min_blocksize = get_bits(&s->gb, 16);
00228 s->max_blocksize = get_bits(&s->gb, 16);
00229
00230 s->min_framesize = get_bits_long(&s->gb, 24);
00231 s->max_framesize = get_bits_long(&s->gb, 24);
00232
00233 s->samplerate = get_bits_long(&s->gb, 20);
00234 s->channels = get_bits(&s->gb, 3) + 1;
00235 s->bps = get_bits(&s->gb, 5) + 1;
00236
00237 s->avctx->channels = s->channels;
00238 s->avctx->sample_rate = s->samplerate;
00239
00240 skip_bits(&s->gb, 36);
00241
00242 skip_bits(&s->gb, 64);
00243 skip_bits(&s->gb, 64);
00244
00245 allocate_buffers(s);
00246 }
00247
00248 static int decode_residuals(FLACContext *s, int channel, int pred_order)
00249 {
00250 int i, tmp, partition, method_type, rice_order;
00251 int sample = 0, samples;
00252
00253 method_type = get_bits(&s->gb, 2);
00254 if (method_type != 0){
00255 av_log(s->avctx, AV_LOG_DEBUG, "illegal residual coding method %d\n", method_type);
00256 return -1;
00257 }
00258
00259 rice_order = get_bits(&s->gb, 4);
00260
00261 samples= s->blocksize >> rice_order;
00262
00263 sample=
00264 i= pred_order;
00265 for (partition = 0; partition < (1 << rice_order); partition++)
00266 {
00267 tmp = get_bits(&s->gb, 4);
00268 if (tmp == 15)
00269 {
00270 av_log(s->avctx, AV_LOG_DEBUG, "fixed len partition\n");
00271 tmp = get_bits(&s->gb, 5);
00272 for (; i < samples; i++, sample++)
00273 s->decoded[channel][sample] = get_sbits(&s->gb, tmp);
00274 }
00275 else
00276 {
00277
00278 for (; i < samples; i++, sample++){
00279 s->decoded[channel][sample] = get_sr_golomb_flac(&s->gb, tmp, INT_MAX, 0);
00280 }
00281 }
00282 i= 0;
00283 }
00284
00285
00286
00287 return 0;
00288 }
00289
00290 static int decode_subframe_fixed(FLACContext *s, int channel, int pred_order)
00291 {
00292 int i;
00293
00294
00295
00296
00297
00298
00299 for (i = 0; i < pred_order; i++)
00300 {
00301 s->decoded[channel][i] = get_sbits(&s->gb, s->curr_bps);
00302
00303 }
00304
00305 if (decode_residuals(s, channel, pred_order) < 0)
00306 return -1;
00307
00308 switch(pred_order)
00309 {
00310 case 0:
00311 break;
00312 case 1:
00313 for (i = pred_order; i < s->blocksize; i++)
00314 s->decoded[channel][i] += s->decoded[channel][i-1];
00315 break;
00316 case 2:
00317 for (i = pred_order; i < s->blocksize; i++)
00318 s->decoded[channel][i] += 2*s->decoded[channel][i-1]
00319 - s->decoded[channel][i-2];
00320 break;
00321 case 3:
00322 for (i = pred_order; i < s->blocksize; i++)
00323 s->decoded[channel][i] += 3*s->decoded[channel][i-1]
00324 - 3*s->decoded[channel][i-2]
00325 + s->decoded[channel][i-3];
00326 break;
00327 case 4:
00328 for (i = pred_order; i < s->blocksize; i++)
00329 s->decoded[channel][i] += 4*s->decoded[channel][i-1]
00330 - 6*s->decoded[channel][i-2]
00331 + 4*s->decoded[channel][i-3]
00332 - s->decoded[channel][i-4];
00333 break;
00334 default:
00335 av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order);
00336 return -1;
00337 }
00338
00339 return 0;
00340 }
00341
00342 static int decode_subframe_lpc(FLACContext *s, int channel, int pred_order)
00343 {
00344 int sum, i, j;
00345 int coeff_prec, qlevel;
00346 int coeffs[pred_order];
00347
00348
00349
00350
00351
00352
00353 for (i = 0; i < pred_order; i++)
00354 {
00355 s->decoded[channel][i] = get_sbits(&s->gb, s->curr_bps);
00356
00357 }
00358
00359 coeff_prec = get_bits(&s->gb, 4) + 1;
00360 if (coeff_prec == 16)
00361 {
00362 av_log(s->avctx, AV_LOG_DEBUG, "invalid coeff precision\n");
00363 return -1;
00364 }
00365
00366 qlevel = get_sbits(&s->gb, 5);
00367
00368 if(qlevel < 0){
00369 av_log(s->avctx, AV_LOG_DEBUG, "qlevel %d not supported, maybe buggy stream\n", qlevel);
00370 return -1;
00371 }
00372
00373 for (i = 0; i < pred_order; i++)
00374 {
00375 coeffs[i] = get_sbits(&s->gb, coeff_prec);
00376
00377 }
00378
00379 if (decode_residuals(s, channel, pred_order) < 0)
00380 return -1;
00381
00382 for (i = pred_order; i < s->blocksize; i++)
00383 {
00384 sum = 0;
00385 for (j = 0; j < pred_order; j++)
00386 sum += coeffs[j] * s->decoded[channel][i-j-1];
00387 s->decoded[channel][i] += sum >> qlevel;
00388 }
00389
00390 return 0;
00391 }
00392
00393 static inline int decode_subframe(FLACContext *s, int channel)
00394 {
00395 int type, wasted = 0;
00396 int i, tmp;
00397
00398 s->curr_bps = s->bps;
00399 if(channel == 0){
00400 if(s->decorrelation == RIGHT_SIDE)
00401 s->curr_bps++;
00402 }else{
00403 if(s->decorrelation == LEFT_SIDE || s->decorrelation == MID_SIDE)
00404 s->curr_bps++;
00405 }
00406
00407 if (get_bits1(&s->gb))
00408 {
00409 av_log(s->avctx, AV_LOG_DEBUG, "invalid subframe padding\n");
00410 return -1;
00411 }
00412 type = get_bits(&s->gb, 6);
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423 #if 0
00424 wasted= 16 - av_log2(show_bits(&s->gb, 17));
00425 skip_bits(&s->gb, wasted+1);
00426 s->curr_bps -= wasted;
00427 #else
00428 if (get_bits1(&s->gb))
00429 {
00430 wasted = 1;
00431 while (!get_bits1(&s->gb))
00432 wasted++;
00433 s->curr_bps -= wasted;
00434 av_log(s->avctx, AV_LOG_DEBUG, "%d wasted bits\n", wasted);
00435 }
00436 #endif
00437
00438 if (type == 0)
00439 {
00440 av_log(s->avctx, AV_LOG_DEBUG, "coding type: constant\n");
00441 tmp = get_sbits(&s->gb, s->curr_bps);
00442 for (i = 0; i < s->blocksize; i++)
00443 s->decoded[channel][i] = tmp;
00444 }
00445 else if (type == 1)
00446 {
00447 av_log(s->avctx, AV_LOG_DEBUG, "coding type: verbatim\n");
00448 for (i = 0; i < s->blocksize; i++)
00449 s->decoded[channel][i] = get_sbits(&s->gb, s->curr_bps);
00450 }
00451 else if ((type >= 8) && (type <= 12))
00452 {
00453
00454 if (decode_subframe_fixed(s, channel, type & ~0x8) < 0)
00455 return -1;
00456 }
00457 else if (type >= 32)
00458 {
00459
00460 if (decode_subframe_lpc(s, channel, (type & ~0x20)+1) < 0)
00461 return -1;
00462 }
00463 else
00464 {
00465 av_log(s->avctx, AV_LOG_DEBUG, "invalid coding type\n");
00466 return -1;
00467 }
00468
00469 if (wasted)
00470 {
00471 int i;
00472 for (i = 0; i < s->blocksize; i++)
00473 s->decoded[channel][i] <<= wasted;
00474 }
00475
00476 return 0;
00477 }
00478
00479 static int decode_frame(FLACContext *s)
00480 {
00481 int blocksize_code, sample_rate_code, sample_size_code, assignment, i, crc8;
00482 int decorrelation, bps, blocksize, samplerate;
00483
00484 blocksize_code = get_bits(&s->gb, 4);
00485
00486 sample_rate_code = get_bits(&s->gb, 4);
00487
00488 assignment = get_bits(&s->gb, 4);
00489 if (assignment < 8 && s->channels == assignment+1)
00490 decorrelation = INDEPENDENT;
00491 else if (assignment >=8 && assignment < 11 && s->channels == 2)
00492 decorrelation = LEFT_SIDE + assignment - 8;
00493 else
00494 {
00495 av_log(s->avctx, AV_LOG_DEBUG, "unsupported channel assignment %d (channels=%d)\n", assignment, s->channels);
00496 return -1;
00497 }
00498
00499 sample_size_code = get_bits(&s->gb, 3);
00500 if(sample_size_code == 0)
00501 bps= s->bps;
00502 else if((sample_size_code != 3) && (sample_size_code != 7))
00503 bps = sample_size_table[sample_size_code];
00504 else
00505 {
00506 av_log(s->avctx, AV_LOG_DEBUG, "invalid sample size code (%d)\n", sample_size_code);
00507 return -1;
00508 }
00509
00510 if (get_bits1(&s->gb))
00511 {
00512 av_log(s->avctx, AV_LOG_DEBUG, "broken stream, invalid padding\n");
00513 return -1;
00514 }
00515
00516 if(get_utf8(&s->gb) < 0){
00517 av_log(s->avctx, AV_LOG_ERROR, "utf8 fscked\n");
00518 return -1;
00519 }
00520 #if 0
00521 if (
00522 (s->min_blocksize != s->max_blocksize)){
00523 }else{
00524 }
00525 #endif
00526
00527 if (blocksize_code == 0)
00528 blocksize = s->min_blocksize;
00529 else if (blocksize_code == 6)
00530 blocksize = get_bits(&s->gb, 8)+1;
00531 else if (blocksize_code == 7)
00532 blocksize = get_bits(&s->gb, 16)+1;
00533 else
00534 blocksize = blocksize_table[blocksize_code];
00535
00536 if(blocksize > s->max_blocksize){
00537 av_log(s->avctx, AV_LOG_ERROR, "blocksize %d > %d\n", blocksize, s->max_blocksize);
00538 return -1;
00539 }
00540
00541 if (sample_rate_code == 0){
00542 samplerate= s->samplerate;
00543 }else if ((sample_rate_code > 3) && (sample_rate_code < 12))
00544 samplerate = sample_rate_table[sample_rate_code];
00545 else if (sample_rate_code == 12)
00546 samplerate = get_bits(&s->gb, 8) * 1000;
00547 else if (sample_rate_code == 13)
00548 samplerate = get_bits(&s->gb, 16);
00549 else if (sample_rate_code == 14)
00550 samplerate = get_bits(&s->gb, 16) * 10;
00551 else{
00552 av_log(s->avctx, AV_LOG_ERROR, "illegal sample rate code %d\n", sample_rate_code);
00553 return -1;
00554 }
00555
00556 skip_bits(&s->gb, 8);
00557 crc8= get_crc8(s->gb.buffer, get_bits_count(&s->gb)/8);
00558 if(crc8){
00559 av_log(s->avctx, AV_LOG_ERROR, "header crc mismatch crc=%2X\n", crc8);
00560 return -1;
00561 }
00562
00563 s->blocksize = blocksize;
00564 s->samplerate = samplerate;
00565 s->bps = bps;
00566 s->decorrelation= decorrelation;
00567
00568
00569
00570
00571 for (i = 0; i < s->channels; i++)
00572 {
00573
00574 if (decode_subframe(s, i) < 0)
00575 return -1;
00576 }
00577
00578 align_get_bits(&s->gb);
00579
00580
00581 skip_bits(&s->gb, 16);
00582
00583 return 0;
00584 }
00585
00586 static int flac_decode_frame(AVCodecContext *avctx,
00587 void *data, int *data_size,
00588 uint8_t *buf, int buf_size)
00589 {
00590 FLACContext *s = avctx->priv_data;
00591 int metadata_last, metadata_type, metadata_size;
00592 int tmp = 0, i, j = 0, input_buf_size = 0;
00593 int16_t *samples = data;
00594
00595 if(s->max_framesize == 0){
00596 s->max_framesize= 65536;
00597 s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);
00598 }
00599
00600 if(1 && s->max_framesize){
00601 buf_size= FFMAX(FFMIN(buf_size, s->max_framesize - s->bitstream_size), 0);
00602 input_buf_size= buf_size;
00603
00604 if(s->bitstream_index + s->bitstream_size + buf_size > s->allocated_bitstream_size){
00605
00606 memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);
00607 s->bitstream_index=0;
00608 }
00609 memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf, buf_size);
00610 buf= &s->bitstream[s->bitstream_index];
00611 buf_size += s->bitstream_size;
00612 s->bitstream_size= buf_size;
00613
00614 if(buf_size < s->max_framesize){
00615
00616 return input_buf_size;
00617 }
00618 }
00619
00620 init_get_bits(&s->gb, buf, buf_size*8);
00621
00622
00623 if (show_bits_long(&s->gb, 32) == bswap_32(ff_get_fourcc("fLaC")))
00624 {
00625 skip_bits(&s->gb, 32);
00626
00627 av_log(s->avctx, AV_LOG_DEBUG, "STREAM HEADER\n");
00628 do {
00629 metadata_last = get_bits(&s->gb, 1);
00630 metadata_type = get_bits(&s->gb, 7);
00631 metadata_size = get_bits_long(&s->gb, 24);
00632
00633 av_log(s->avctx, AV_LOG_DEBUG, " metadata block: flag = %d, type = %d, size = %d\n",
00634 metadata_last, metadata_type,
00635 metadata_size);
00636 if(metadata_size){
00637 switch(metadata_type)
00638 {
00639 case METADATA_TYPE_STREAMINFO:{
00640 metadata_streaminfo(s);
00641
00642
00643 if(buf != &s->bitstream[s->bitstream_index])
00644 {
00645 int bits_count = get_bits_count(&s->gb);
00646 buf= &s->bitstream[s->bitstream_index];
00647 init_get_bits(&s->gb, buf, buf_size*8);
00648 skip_bits(&s->gb, bits_count);
00649 }
00650
00651 dump_headers(s);
00652 break;}
00653 default:
00654 for(i=0; i<metadata_size; i++)
00655 skip_bits(&s->gb, 8);
00656 }
00657 }
00658 } while(!metadata_last);
00659 }
00660 else
00661 {
00662
00663 tmp = show_bits(&s->gb, 16);
00664 if(tmp != 0xFFF8){
00665 av_log(s->avctx, AV_LOG_ERROR, "FRAME HEADER not here\n");
00666 while(get_bits_count(&s->gb)/8+2 < buf_size && show_bits(&s->gb, 16) != 0xFFF8)
00667 skip_bits(&s->gb, 8);
00668 goto end;
00669 }
00670 skip_bits(&s->gb, 16);
00671 if (decode_frame(s) < 0){
00672 av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n");
00673 s->bitstream_size=0;
00674 s->bitstream_index=0;
00675 return -1;
00676 }
00677 }
00678
00679
00680 #if 0
00681
00682 if (s->order == MID_SIDE)
00683 {
00684 short *left = samples;
00685 short *right = samples + s->blocksize;
00686 for (i = 0; i < s->blocksize; i += 2)
00687 {
00688 uint32_t x = s->decoded[0][i];
00689 uint32_t y = s->decoded[0][i+1];
00690
00691 right[i] = x - (y / 2);
00692 left[i] = right[i] + y;
00693 }
00694 *data_size = 2 * s->blocksize;
00695 }
00696 else
00697 {
00698 for (i = 0; i < s->channels; i++)
00699 {
00700 switch(s->order)
00701 {
00702 case INDEPENDENT:
00703 for (j = 0; j < s->blocksize; j++)
00704 samples[(s->blocksize*i)+j] = s->decoded[i][j];
00705 break;
00706 case LEFT_SIDE:
00707 case RIGHT_SIDE:
00708 if (i == 0)
00709 for (j = 0; j < s->blocksize; j++)
00710 samples[(s->blocksize*i)+j] = s->decoded[0][j];
00711 else
00712 for (j = 0; j < s->blocksize; j++)
00713 samples[(s->blocksize*i)+j] = s->decoded[0][j] - s->decoded[i][j];
00714 break;
00715
00716
00717 }
00718 *data_size += s->blocksize;
00719 }
00720 }
00721 #else
00722 switch(s->decorrelation)
00723 {
00724 case INDEPENDENT:
00725 for (j = 0; j < s->blocksize; j++)
00726 {
00727 for (i = 0; i < s->channels; i++)
00728 *(samples++) = s->decoded[i][j];
00729 }
00730 break;
00731 case LEFT_SIDE:
00732 assert(s->channels == 2);
00733 for (i = 0; i < s->blocksize; i++)
00734 {
00735 *(samples++) = s->decoded[0][i];
00736 *(samples++) = s->decoded[0][i] - s->decoded[1][i];
00737 }
00738 break;
00739 case RIGHT_SIDE:
00740 assert(s->channels == 2);
00741 for (i = 0; i < s->blocksize; i++)
00742 {
00743 *(samples++) = s->decoded[0][i] + s->decoded[1][i];
00744 *(samples++) = s->decoded[1][i];
00745 }
00746 break;
00747 case MID_SIDE:
00748 assert(s->channels == 2);
00749 for (i = 0; i < s->blocksize; i++)
00750 {
00751 int mid, side;
00752 mid = s->decoded[0][i];
00753 side = s->decoded[1][i];
00754
00755 #if 1 //needs to be checked but IMHO it should be binary identical
00756 mid -= side>>1;
00757 *(samples++) = mid + side;
00758 *(samples++) = mid;
00759 #else
00760
00761 mid <<= 1;
00762 if (side & 1)
00763 mid++;
00764 *(samples++) = (mid + side) >> 1;
00765 *(samples++) = (mid - side) >> 1;
00766 #endif
00767 }
00768 break;
00769 }
00770 #endif
00771
00772 *data_size = (int8_t *)samples - (int8_t *)data;
00773
00774
00775
00776 end:
00777 i= (get_bits_count(&s->gb)+7)/8;;
00778 if(i > buf_size){
00779 av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
00780 s->bitstream_size=0;
00781 s->bitstream_index=0;
00782 return -1;
00783 }
00784
00785 if(s->bitstream_size){
00786 s->bitstream_index += i;
00787 s->bitstream_size -= i;
00788 return input_buf_size;
00789 }else
00790 return i;
00791 }
00792
00793 static int flac_decode_close(AVCodecContext *avctx)
00794 {
00795 FLACContext *s = avctx->priv_data;
00796 int i;
00797
00798 for (i = 0; i < s->channels; i++)
00799 {
00800 av_freep(&s->decoded[i]);
00801 }
00802 av_freep(&s->bitstream);
00803
00804 return 0;
00805 }
00806
00807 static void flac_flush(AVCodecContext *avctx){
00808 FLACContext *s = avctx->priv_data;
00809
00810 s->bitstream_size=
00811 s->bitstream_index= 0;
00812 }
00813
00814 AVCodec flac_decoder = {
00815 "flac",
00816 CODEC_TYPE_AUDIO,
00817 CODEC_ID_FLAC,
00818 sizeof(FLACContext),
00819 flac_decode_init,
00820 NULL,
00821 flac_decode_close,
00822 flac_decode_frame,
00823 .flush= flac_flush,
00824 };