00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00048 #include "libavutil/audioconvert.h"
00049 #include "avcodec.h"
00050 #include "get_bits.h"
00051 #include "bytestream.h"
00052 #include "unary.h"
00053 #include "mathops.h"
00054 
00055 #define ALAC_EXTRADATA_SIZE 36
00056 #define MAX_CHANNELS 8
00057 
00058 typedef struct {
00059     AVCodecContext *avctx;
00060     AVFrame frame;
00061     GetBitContext gb;
00062     int channels;
00063 
00064     int32_t *predict_error_buffer[2];
00065     int32_t *output_samples_buffer[2];
00066     int32_t *extra_bits_buffer[2];
00067 
00068     uint32_t max_samples_per_frame;
00069     uint8_t  sample_size;
00070     uint8_t  rice_history_mult;
00071     uint8_t  rice_initial_history;
00072     uint8_t  rice_limit;
00073 
00074     int extra_bits;     
00075     int nb_samples;     
00077     int direct_output;
00078 } ALACContext;
00079 
00080 enum RawDataBlockType {
00081     
00082     TYPE_SCE,
00083     TYPE_CPE,
00084     TYPE_CCE,
00085     TYPE_LFE,
00086     TYPE_DSE,
00087     TYPE_PCE,
00088     TYPE_FIL,
00089     TYPE_END
00090 };
00091 
00092 static const uint8_t alac_channel_layout_offsets[8][8] = {
00093     { 0 },
00094     { 0, 1 },
00095     { 2, 0, 1 },
00096     { 2, 0, 1, 3 },
00097     { 2, 0, 1, 3, 4 },
00098     { 2, 0, 1, 4, 5, 3 },
00099     { 2, 0, 1, 4, 5, 6, 3 },
00100     { 2, 6, 7, 0, 1, 4, 5, 3 }
00101 };
00102 
00103 static const uint16_t alac_channel_layouts[8] = {
00104     AV_CH_LAYOUT_MONO,
00105     AV_CH_LAYOUT_STEREO,
00106     AV_CH_LAYOUT_SURROUND,
00107     AV_CH_LAYOUT_4POINT0,
00108     AV_CH_LAYOUT_5POINT0_BACK,
00109     AV_CH_LAYOUT_5POINT1_BACK,
00110     AV_CH_LAYOUT_6POINT1_BACK,
00111     AV_CH_LAYOUT_7POINT1_WIDE_BACK
00112 };
00113 
00114 static inline unsigned int decode_scalar(GetBitContext *gb, int k, int bps)
00115 {
00116     unsigned int x = get_unary_0_9(gb);
00117 
00118     if (x > 8) { 
00119         
00120         x = get_bits_long(gb, bps);
00121     } else if (k != 1) {
00122         int extrabits = show_bits(gb, k);
00123 
00124         
00125         x = (x << k) - x;
00126 
00127         if (extrabits > 1) {
00128             x += extrabits - 1;
00129             skip_bits(gb, k);
00130         } else
00131             skip_bits(gb, k - 1);
00132     }
00133     return x;
00134 }
00135 
00136 static int rice_decompress(ALACContext *alac, int32_t *output_buffer,
00137                             int nb_samples, int bps, int rice_history_mult)
00138 {
00139     int i;
00140     unsigned int history = alac->rice_initial_history;
00141     int sign_modifier = 0;
00142 
00143     for (i = 0; i < nb_samples; i++) {
00144         int k;
00145         unsigned int x;
00146 
00147         if(get_bits_left(&alac->gb) <= 0)
00148             return -1;
00149 
00150         
00151         k = av_log2((history >> 9) + 3);
00152         k = FFMIN(k, alac->rice_limit);
00153         x = decode_scalar(&alac->gb, k, bps);
00154         x += sign_modifier;
00155         sign_modifier = 0;
00156         output_buffer[i] = (x >> 1) ^ -(x & 1);
00157 
00158         
00159         if (x > 0xffff)
00160             history = 0xffff;
00161         else
00162             history +=         x * rice_history_mult -
00163                        ((history * rice_history_mult) >> 9);
00164 
00165         
00166         if ((history < 128) && (i + 1 < nb_samples)) {
00167             int block_size;
00168 
00169             
00170             k = 7 - av_log2(history) + ((history + 16) >> 6);
00171             k = FFMIN(k, alac->rice_limit);
00172             block_size = decode_scalar(&alac->gb, k, 16);
00173 
00174             if (block_size > 0) {
00175                 if (block_size >= nb_samples - i) {
00176                     av_log(alac->avctx, AV_LOG_ERROR,
00177                            "invalid zero block size of %d %d %d\n", block_size,
00178                            nb_samples, i);
00179                     block_size = nb_samples - i - 1;
00180                 }
00181                 memset(&output_buffer[i + 1], 0,
00182                        block_size * sizeof(*output_buffer));
00183                 i += block_size;
00184             }
00185             if (block_size <= 0xffff)
00186                 sign_modifier = 1;
00187             history = 0;
00188         }
00189     }
00190     return 0;
00191 }
00192 
00193 static inline int sign_only(int v)
00194 {
00195     return v ? FFSIGN(v) : 0;
00196 }
00197 
00198 static void lpc_prediction(int32_t *error_buffer, int32_t *buffer_out,
00199                            int nb_samples, int bps, int16_t *lpc_coefs,
00200                            int lpc_order, int lpc_quant)
00201 {
00202     int i;
00203     int32_t *pred = buffer_out;
00204 
00205     
00206     *buffer_out = *error_buffer;
00207 
00208     if (nb_samples <= 1)
00209         return;
00210 
00211     if (!lpc_order) {
00212         memcpy(&buffer_out[1], &error_buffer[1],
00213                (nb_samples - 1) * sizeof(*buffer_out));
00214         return;
00215     }
00216 
00217     if (lpc_order == 31) {
00218         
00219         for (i = 1; i < nb_samples; i++) {
00220             buffer_out[i] = sign_extend(buffer_out[i - 1] + error_buffer[i],
00221                                         bps);
00222         }
00223         return;
00224     }
00225 
00226     
00227     for (i = 1; i <= lpc_order; i++)
00228         buffer_out[i] = sign_extend(buffer_out[i - 1] + error_buffer[i], bps);
00229 
00230     
00231 
00232     for (; i < nb_samples; i++) {
00233         int j;
00234         int val = 0;
00235         int error_val = error_buffer[i];
00236         int error_sign;
00237         int d = *pred++;
00238 
00239         
00240         for (j = 0; j < lpc_order; j++)
00241             val += (pred[j] - d) * lpc_coefs[j];
00242         val = (val + (1 << (lpc_quant - 1))) >> lpc_quant;
00243         val += d + error_val;
00244         buffer_out[i] = sign_extend(val, bps);
00245 
00246         
00247         error_sign = sign_only(error_val);
00248         if (error_sign) {
00249             for (j = 0; j < lpc_order && error_val * error_sign > 0; j++) {
00250                 int sign;
00251                 val  = d - pred[j];
00252                 sign = sign_only(val) * error_sign;
00253                 lpc_coefs[j] -= sign;
00254                 val *= sign;
00255                 error_val -= (val >> lpc_quant) * (j + 1);
00256             }
00257         }
00258     }
00259 }
00260 
00261 static void decorrelate_stereo(int32_t *buffer[2], int nb_samples,
00262                                int decorr_shift, int decorr_left_weight)
00263 {
00264     int i;
00265 
00266     for (i = 0; i < nb_samples; i++) {
00267         int32_t a, b;
00268 
00269         a = buffer[0][i];
00270         b = buffer[1][i];
00271 
00272         a -= (b * decorr_left_weight) >> decorr_shift;
00273         b += a;
00274 
00275         buffer[0][i] = b;
00276         buffer[1][i] = a;
00277     }
00278 }
00279 
00280 static void append_extra_bits(int32_t *buffer[2], int32_t *extra_bits_buffer[2],
00281                               int extra_bits, int channels, int nb_samples)
00282 {
00283     int i, ch;
00284 
00285     for (ch = 0; ch < channels; ch++)
00286         for (i = 0; i < nb_samples; i++)
00287             buffer[ch][i] = (buffer[ch][i] << extra_bits) | extra_bits_buffer[ch][i];
00288 }
00289 
00290 static int decode_element(AVCodecContext *avctx, void *data, int ch_index,
00291                           int channels)
00292 {
00293     ALACContext *alac = avctx->priv_data;
00294     int has_size, bps, is_compressed, decorr_shift, decorr_left_weight, ret;
00295     uint32_t output_samples;
00296     int i, ch;
00297 
00298     skip_bits(&alac->gb, 4);  
00299     skip_bits(&alac->gb, 12); 
00300 
00301     
00302     has_size = get_bits1(&alac->gb);
00303 
00304     alac->extra_bits = get_bits(&alac->gb, 2) << 3;
00305     bps = alac->sample_size - alac->extra_bits + channels - 1;
00306     if (bps > 32) {
00307         av_log(avctx, AV_LOG_ERROR, "bps is unsupported: %d\n", bps);
00308         return AVERROR_PATCHWELCOME;
00309     }
00310 
00311     
00312     is_compressed = !get_bits1(&alac->gb);
00313 
00314     if (has_size)
00315         output_samples = get_bits_long(&alac->gb, 32);
00316     else
00317         output_samples = alac->max_samples_per_frame;
00318     if (!output_samples || output_samples > alac->max_samples_per_frame) {
00319         av_log(avctx, AV_LOG_ERROR, "invalid samples per frame: %d\n",
00320                output_samples);
00321         return AVERROR_INVALIDDATA;
00322     }
00323     if (!alac->nb_samples) {
00324         
00325         alac->frame.nb_samples = output_samples;
00326         if ((ret = avctx->get_buffer(avctx, &alac->frame)) < 0) {
00327             av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00328             return ret;
00329         }
00330     } else if (output_samples != alac->nb_samples) {
00331         av_log(avctx, AV_LOG_ERROR, "sample count mismatch: %u != %d\n",
00332                output_samples, alac->nb_samples);
00333         return AVERROR_INVALIDDATA;
00334     }
00335     alac->nb_samples = output_samples;
00336     if (alac->direct_output) {
00337         for (ch = 0; ch < channels; ch++)
00338             alac->output_samples_buffer[ch] = (int32_t *)alac->frame.extended_data[ch_index + ch];
00339     }
00340 
00341     if (is_compressed) {
00342         int16_t lpc_coefs[2][32];
00343         int lpc_order[2];
00344         int prediction_type[2];
00345         int lpc_quant[2];
00346         int rice_history_mult[2];
00347 
00348         decorr_shift       = get_bits(&alac->gb, 8);
00349         decorr_left_weight = get_bits(&alac->gb, 8);
00350 
00351         for (ch = 0; ch < channels; ch++) {
00352             prediction_type[ch]   = get_bits(&alac->gb, 4);
00353             lpc_quant[ch]         = get_bits(&alac->gb, 4);
00354             rice_history_mult[ch] = get_bits(&alac->gb, 3);
00355             lpc_order[ch]         = get_bits(&alac->gb, 5);
00356 
00357             
00358             for (i = lpc_order[ch] - 1; i >= 0; i--)
00359                 lpc_coefs[ch][i] = get_sbits(&alac->gb, 16);
00360         }
00361 
00362         if (alac->extra_bits) {
00363             for (i = 0; i < alac->nb_samples; i++) {
00364                 if(get_bits_left(&alac->gb) <= 0)
00365                     return -1;
00366                 for (ch = 0; ch < channels; ch++)
00367                     alac->extra_bits_buffer[ch][i] = get_bits(&alac->gb, alac->extra_bits);
00368             }
00369         }
00370         for (ch = 0; ch < channels; ch++) {
00371             int ret=rice_decompress(alac, alac->predict_error_buffer[ch],
00372                             alac->nb_samples, bps,
00373                             rice_history_mult[ch] * alac->rice_history_mult / 4);
00374             if(ret<0)
00375                 return ret;
00376 
00377             
00378             if (prediction_type[ch] == 15) {
00379                 
00380 
00381 
00382 
00383 
00384 
00385 
00386                 lpc_prediction(alac->predict_error_buffer[ch],
00387                                alac->predict_error_buffer[ch],
00388                                alac->nb_samples, bps, NULL, 31, 0);
00389             } else if (prediction_type[ch] > 0) {
00390                 av_log(avctx, AV_LOG_WARNING, "unknown prediction type: %i\n",
00391                        prediction_type[ch]);
00392             }
00393             lpc_prediction(alac->predict_error_buffer[ch],
00394                            alac->output_samples_buffer[ch], alac->nb_samples,
00395                            bps, lpc_coefs[ch], lpc_order[ch], lpc_quant[ch]);
00396         }
00397     } else {
00398         
00399         for (i = 0; i < alac->nb_samples; i++) {
00400             if(get_bits_left(&alac->gb) <= 0)
00401                 return -1;
00402             for (ch = 0; ch < channels; ch++) {
00403                 alac->output_samples_buffer[ch][i] =
00404                          get_sbits_long(&alac->gb, alac->sample_size);
00405             }
00406         }
00407         alac->extra_bits   = 0;
00408         decorr_shift       = 0;
00409         decorr_left_weight = 0;
00410     }
00411 
00412     if (channels == 2 && decorr_left_weight) {
00413         decorrelate_stereo(alac->output_samples_buffer, alac->nb_samples,
00414                            decorr_shift, decorr_left_weight);
00415     }
00416 
00417     if (alac->extra_bits) {
00418         append_extra_bits(alac->output_samples_buffer, alac->extra_bits_buffer,
00419                           alac->extra_bits, channels, alac->nb_samples);
00420     }
00421 
00422     if(av_sample_fmt_is_planar(avctx->sample_fmt)) {
00423     switch(alac->sample_size) {
00424     case 16: {
00425         for (ch = 0; ch < channels; ch++) {
00426             int16_t *outbuffer = (int16_t *)alac->frame.extended_data[ch_index + ch];
00427             for (i = 0; i < alac->nb_samples; i++)
00428                 *outbuffer++ = alac->output_samples_buffer[ch][i];
00429         }}
00430         break;
00431     case 24: {
00432         for (ch = 0; ch < channels; ch++) {
00433             for (i = 0; i < alac->nb_samples; i++)
00434                 alac->output_samples_buffer[ch][i] <<= 8;
00435         }}
00436         break;
00437     }
00438     }else{
00439         switch(alac->sample_size) {
00440         case 16: {
00441             int16_t *outbuffer = ((int16_t *)alac->frame.extended_data[0]) + ch_index;
00442             for (i = 0; i < alac->nb_samples; i++) {
00443                 for (ch = 0; ch < channels; ch++)
00444                     *outbuffer++ = alac->output_samples_buffer[ch][i];
00445                 outbuffer += alac->channels - channels;
00446             }
00447             }
00448             break;
00449         case 24: {
00450             int32_t *outbuffer = ((int32_t *)alac->frame.extended_data[0]) + ch_index;
00451             for (i = 0; i < alac->nb_samples; i++) {
00452                 for (ch = 0; ch < channels; ch++)
00453                     *outbuffer++ = alac->output_samples_buffer[ch][i] << 8;
00454                 outbuffer += alac->channels - channels;
00455             }
00456             }
00457             break;
00458         case 32: {
00459             int32_t *outbuffer = ((int32_t *)alac->frame.extended_data[0]) + ch_index;
00460             for (i = 0; i < alac->nb_samples; i++) {
00461                 for (ch = 0; ch < channels; ch++)
00462                     *outbuffer++ = alac->output_samples_buffer[ch][i];
00463                 outbuffer += alac->channels - channels;
00464             }
00465             }
00466             break;
00467         }
00468     }
00469 
00470     return 0;
00471 }
00472 
00473 static int alac_decode_frame(AVCodecContext *avctx, void *data,
00474                              int *got_frame_ptr, AVPacket *avpkt)
00475 {
00476     ALACContext *alac = avctx->priv_data;
00477     enum RawDataBlockType element;
00478     int channels;
00479     int ch, ret, got_end;
00480 
00481     init_get_bits(&alac->gb, avpkt->data, avpkt->size * 8);
00482 
00483     got_end = 0;
00484     alac->nb_samples = 0;
00485     ch = 0;
00486     while (get_bits_left(&alac->gb) >= 3) {
00487         element = get_bits(&alac->gb, 3);
00488         if (element == TYPE_END) {
00489             got_end = 1;
00490             break;
00491         }
00492         if (element > TYPE_CPE && element != TYPE_LFE) {
00493             av_log(avctx, AV_LOG_ERROR, "syntax element unsupported: %d\n", element);
00494             return AVERROR_PATCHWELCOME;
00495         }
00496 
00497         channels = (element == TYPE_CPE) ? 2 : 1;
00498         if (ch + channels > alac->channels) {
00499             av_log(avctx, AV_LOG_ERROR, "invalid element channel count\n");
00500             return AVERROR_INVALIDDATA;
00501         }
00502 
00503         ret = decode_element(avctx, data,
00504                              alac_channel_layout_offsets[alac->channels - 1][ch],
00505                              channels);
00506         if (ret < 0 && get_bits_left(&alac->gb))
00507             return ret;
00508 
00509         ch += channels;
00510     }
00511     if (!got_end) {
00512         av_log(avctx, AV_LOG_ERROR, "no end tag found. incomplete packet.\n");
00513         return AVERROR_INVALIDDATA;
00514     }
00515 
00516     if (avpkt->size * 8 - get_bits_count(&alac->gb) > 8) {
00517         av_log(avctx, AV_LOG_ERROR, "Error : %d bits left\n",
00518                avpkt->size * 8 - get_bits_count(&alac->gb));
00519     }
00520 
00521     *got_frame_ptr   = 1;
00522     *(AVFrame *)data = alac->frame;
00523 
00524     return avpkt->size;
00525 }
00526 
00527 static av_cold int alac_decode_close(AVCodecContext *avctx)
00528 {
00529     ALACContext *alac = avctx->priv_data;
00530 
00531     int ch;
00532     for (ch = 0; ch < FFMIN(alac->channels, 2); ch++) {
00533         av_freep(&alac->predict_error_buffer[ch]);
00534         if (!alac->direct_output)
00535             av_freep(&alac->output_samples_buffer[ch]);
00536         av_freep(&alac->extra_bits_buffer[ch]);
00537     }
00538 
00539     return 0;
00540 }
00541 
00542 static int allocate_buffers(ALACContext *alac)
00543 {
00544     int ch;
00545     int buf_size = alac->max_samples_per_frame * sizeof(int32_t);
00546 
00547     for (ch = 0; ch < FFMIN(alac->channels, 2); ch++) {
00548         FF_ALLOC_OR_GOTO(alac->avctx, alac->predict_error_buffer[ch],
00549                          buf_size, buf_alloc_fail);
00550 
00551         alac->direct_output = alac->sample_size > 16 && av_sample_fmt_is_planar(alac->avctx->sample_fmt);
00552         if (!alac->direct_output) {
00553             FF_ALLOC_OR_GOTO(alac->avctx, alac->output_samples_buffer[ch],
00554                              buf_size, buf_alloc_fail);
00555         }
00556 
00557         FF_ALLOC_OR_GOTO(alac->avctx, alac->extra_bits_buffer[ch],
00558                          buf_size, buf_alloc_fail);
00559     }
00560     return 0;
00561 buf_alloc_fail:
00562     alac_decode_close(alac->avctx);
00563     return AVERROR(ENOMEM);
00564 }
00565 
00566 static int alac_set_info(ALACContext *alac)
00567 {
00568     GetByteContext gb;
00569 
00570     bytestream2_init(&gb, alac->avctx->extradata,
00571                      alac->avctx->extradata_size);
00572 
00573     bytestream2_skipu(&gb, 12); 
00574 
00575     alac->max_samples_per_frame = bytestream2_get_be32u(&gb);
00576     if (!alac->max_samples_per_frame || alac->max_samples_per_frame > INT_MAX) {
00577         av_log(alac->avctx, AV_LOG_ERROR, "max samples per frame invalid: %u\n",
00578                alac->max_samples_per_frame);
00579         return AVERROR_INVALIDDATA;
00580     }
00581     bytestream2_skipu(&gb, 1);  
00582     alac->sample_size          = bytestream2_get_byteu(&gb);
00583     alac->rice_history_mult    = bytestream2_get_byteu(&gb);
00584     alac->rice_initial_history = bytestream2_get_byteu(&gb);
00585     alac->rice_limit           = bytestream2_get_byteu(&gb);
00586     alac->channels             = bytestream2_get_byteu(&gb);
00587     bytestream2_get_be16u(&gb); 
00588     bytestream2_get_be32u(&gb); 
00589     bytestream2_get_be32u(&gb); 
00590     bytestream2_get_be32u(&gb); 
00591 
00592     return 0;
00593 }
00594 
00595 static av_cold int alac_decode_init(AVCodecContext * avctx)
00596 {
00597     int ret;
00598     int req_packed;
00599     ALACContext *alac = avctx->priv_data;
00600     alac->avctx = avctx;
00601 
00602     
00603     if (alac->avctx->extradata_size != ALAC_EXTRADATA_SIZE) {
00604         av_log(avctx, AV_LOG_ERROR, "expected %d extradata bytes\n",
00605             ALAC_EXTRADATA_SIZE);
00606         return -1;
00607     }
00608     if (alac_set_info(alac)) {
00609         av_log(avctx, AV_LOG_ERROR, "set_info failed\n");
00610         return -1;
00611     }
00612 
00613     req_packed = LIBAVCODEC_VERSION_MAJOR < 55 && !av_sample_fmt_is_planar(avctx->request_sample_fmt);
00614     switch (alac->sample_size) {
00615     case 16: avctx->sample_fmt = req_packed ? AV_SAMPLE_FMT_S16 : AV_SAMPLE_FMT_S16P;
00616              break;
00617     case 24:
00618     case 32: avctx->sample_fmt = req_packed ? AV_SAMPLE_FMT_S32 : AV_SAMPLE_FMT_S32P;
00619              break;
00620     default: av_log_ask_for_sample(avctx, "Sample depth %d is not supported.\n",
00621                                    alac->sample_size);
00622              return AVERROR_PATCHWELCOME;
00623     }
00624 
00625     if (alac->channels < 1) {
00626         av_log(avctx, AV_LOG_WARNING, "Invalid channel count\n");
00627         alac->channels = avctx->channels;
00628     } else {
00629         if (alac->channels > MAX_CHANNELS)
00630             alac->channels = avctx->channels;
00631         else
00632             avctx->channels = alac->channels;
00633     }
00634     if (avctx->channels > MAX_CHANNELS) {
00635         av_log(avctx, AV_LOG_ERROR, "Unsupported channel count: %d\n",
00636                avctx->channels);
00637         return AVERROR_PATCHWELCOME;
00638     }
00639     avctx->channel_layout = alac_channel_layouts[alac->channels - 1];
00640 
00641     if ((ret = allocate_buffers(alac)) < 0) {
00642         av_log(avctx, AV_LOG_ERROR, "Error allocating buffers\n");
00643         return ret;
00644     }
00645 
00646     avcodec_get_frame_defaults(&alac->frame);
00647     avctx->coded_frame = &alac->frame;
00648 
00649     return 0;
00650 }
00651 
00652 AVCodec ff_alac_decoder = {
00653     .name           = "alac",
00654     .type           = AVMEDIA_TYPE_AUDIO,
00655     .id             = AV_CODEC_ID_ALAC,
00656     .priv_data_size = sizeof(ALACContext),
00657     .init           = alac_decode_init,
00658     .close          = alac_decode_close,
00659     .decode         = alac_decode_frame,
00660     .capabilities   = CODEC_CAP_DR1,
00661     .long_name      = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),
00662 };