• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • Examples
  • File List
  • Globals

libavcodec/qcelpdec.c

Go to the documentation of this file.
00001 /*
00002  * QCELP decoder
00003  * Copyright (c) 2007 Reynaldo H. Verdejo Pinochet
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * Libav is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00030 #include <stddef.h>
00031 
00032 #include "avcodec.h"
00033 #include "internal.h"
00034 #include "get_bits.h"
00035 
00036 #include "qcelpdata.h"
00037 
00038 #include "celp_math.h"
00039 #include "celp_filters.h"
00040 #include "acelp_filters.h"
00041 #include "acelp_vectors.h"
00042 #include "lsp.h"
00043 
00044 #undef NDEBUG
00045 #include <assert.h>
00046 
00047 typedef enum {
00048     I_F_Q = -1,    
00049     SILENCE,
00050     RATE_OCTAVE,
00051     RATE_QUARTER,
00052     RATE_HALF,
00053     RATE_FULL
00054 } qcelp_packet_rate;
00055 
00056 typedef struct {
00057     AVFrame           avframe;
00058     GetBitContext     gb;
00059     qcelp_packet_rate bitrate;
00060     QCELPFrame        frame;    
00062     uint8_t  erasure_count;
00063     uint8_t  octave_count;      
00064     float    prev_lspf[10];
00065     float    predictor_lspf[10];
00066     float    pitch_synthesis_filter_mem[303];
00067     float    pitch_pre_filter_mem[303];
00068     float    rnd_fir_filter_mem[180];
00069     float    formant_mem[170];
00070     float    last_codebook_gain;
00071     int      prev_g1[2];
00072     int      prev_bitrate;
00073     float    pitch_gain[4];
00074     uint8_t  pitch_lag[4];
00075     uint16_t first16bits;
00076     uint8_t  warned_buf_mismatch_bitrate;
00077 
00078     /* postfilter */
00079     float    postfilter_synth_mem[10];
00080     float    postfilter_agc_mem;
00081     float    postfilter_tilt_mem;
00082 } QCELPContext;
00083 
00089 static av_cold int qcelp_decode_init(AVCodecContext *avctx)
00090 {
00091     QCELPContext *q = avctx->priv_data;
00092     int i;
00093 
00094     avctx->sample_fmt = AV_SAMPLE_FMT_FLT;
00095 
00096     for (i = 0; i < 10; i++)
00097         q->prev_lspf[i] = (i + 1) / 11.;
00098 
00099     avcodec_get_frame_defaults(&q->avframe);
00100     avctx->coded_frame = &q->avframe;
00101 
00102     return 0;
00103 }
00104 
00116 static int decode_lspf(QCELPContext *q, float *lspf)
00117 {
00118     int i;
00119     float tmp_lspf, smooth, erasure_coeff;
00120     const float *predictors;
00121 
00122     if (q->bitrate == RATE_OCTAVE || q->bitrate == I_F_Q) {
00123         predictors = q->prev_bitrate != RATE_OCTAVE &&
00124                      q->prev_bitrate != I_F_Q ? q->prev_lspf
00125                                               : q->predictor_lspf;
00126 
00127         if (q->bitrate == RATE_OCTAVE) {
00128             q->octave_count++;
00129 
00130             for (i = 0; i < 10; i++) {
00131                 q->predictor_lspf[i] =
00132                              lspf[i] = (q->frame.lspv[i] ?  QCELP_LSP_SPREAD_FACTOR
00133                                                          : -QCELP_LSP_SPREAD_FACTOR) +
00134                                         predictors[i] * QCELP_LSP_OCTAVE_PREDICTOR   +
00135                                         (i + 1) * ((1 - QCELP_LSP_OCTAVE_PREDICTOR) / 11);
00136             }
00137             smooth = q->octave_count < 10 ? .875 : 0.1;
00138         } else {
00139             erasure_coeff = QCELP_LSP_OCTAVE_PREDICTOR;
00140 
00141             assert(q->bitrate == I_F_Q);
00142 
00143             if (q->erasure_count > 1)
00144                 erasure_coeff *= q->erasure_count < 4 ? 0.9 : 0.7;
00145 
00146             for (i = 0; i < 10; i++) {
00147                 q->predictor_lspf[i] =
00148                              lspf[i] = (i + 1) * (1 - erasure_coeff) / 11 +
00149                                        erasure_coeff * predictors[i];
00150             }
00151             smooth = 0.125;
00152         }
00153 
00154         // Check the stability of the LSP frequencies.
00155         lspf[0] = FFMAX(lspf[0], QCELP_LSP_SPREAD_FACTOR);
00156         for (i = 1; i < 10; i++)
00157             lspf[i] = FFMAX(lspf[i], lspf[i - 1] + QCELP_LSP_SPREAD_FACTOR);
00158 
00159         lspf[9] = FFMIN(lspf[9], 1.0 - QCELP_LSP_SPREAD_FACTOR);
00160         for (i = 9; i > 0; i--)
00161             lspf[i - 1] = FFMIN(lspf[i - 1], lspf[i] - QCELP_LSP_SPREAD_FACTOR);
00162 
00163         // Low-pass filter the LSP frequencies.
00164         ff_weighted_vector_sumf(lspf, lspf, q->prev_lspf, smooth, 1.0 - smooth, 10);
00165     } else {
00166         q->octave_count = 0;
00167 
00168         tmp_lspf = 0.;
00169         for (i = 0; i < 5; i++) {
00170             lspf[2 * i + 0] = tmp_lspf += qcelp_lspvq[i][q->frame.lspv[i]][0] * 0.0001;
00171             lspf[2 * i + 1] = tmp_lspf += qcelp_lspvq[i][q->frame.lspv[i]][1] * 0.0001;
00172         }
00173 
00174         // Check for badly received packets.
00175         if (q->bitrate == RATE_QUARTER) {
00176             if (lspf[9] <= .70 || lspf[9] >= .97)
00177                 return -1;
00178             for (i = 3; i < 10; i++)
00179                 if (fabs(lspf[i] - lspf[i - 2]) < .08)
00180                     return -1;
00181         } else {
00182             if (lspf[9] <= .66 || lspf[9] >= .985)
00183                 return -1;
00184             for (i = 4; i < 10; i++)
00185                 if (fabs(lspf[i] - lspf[i - 4]) < .0931)
00186                     return -1;
00187         }
00188     }
00189     return 0;
00190 }
00191 
00200 static void decode_gain_and_index(QCELPContext *q, float *gain)
00201 {
00202     int i, subframes_count, g1[16];
00203     float slope;
00204 
00205     if (q->bitrate >= RATE_QUARTER) {
00206         switch (q->bitrate) {
00207         case RATE_FULL: subframes_count = 16; break;
00208         case RATE_HALF: subframes_count =  4; break;
00209         default:        subframes_count =  5;
00210         }
00211         for (i = 0; i < subframes_count; i++) {
00212             g1[i] = 4 * q->frame.cbgain[i];
00213             if (q->bitrate == RATE_FULL && !((i + 1) & 3)) {
00214                 g1[i] += av_clip((g1[i - 1] + g1[i - 2] + g1[i - 3]) / 3 - 6, 0, 32);
00215             }
00216 
00217             gain[i] = qcelp_g12ga[g1[i]];
00218 
00219             if (q->frame.cbsign[i]) {
00220                 gain[i] = -gain[i];
00221                 q->frame.cindex[i] = (q->frame.cindex[i] - 89) & 127;
00222             }
00223         }
00224 
00225         q->prev_g1[0]         = g1[i - 2];
00226         q->prev_g1[1]         = g1[i - 1];
00227         q->last_codebook_gain = qcelp_g12ga[g1[i - 1]];
00228 
00229         if (q->bitrate == RATE_QUARTER) {
00230             // Provide smoothing of the unvoiced excitation energy.
00231             gain[7] =       gain[4];
00232             gain[6] = 0.4 * gain[3] + 0.6 * gain[4];
00233             gain[5] =       gain[3];
00234             gain[4] = 0.8 * gain[2] + 0.2 * gain[3];
00235             gain[3] = 0.2 * gain[1] + 0.8 * gain[2];
00236             gain[2] =       gain[1];
00237             gain[1] = 0.6 * gain[0] + 0.4 * gain[1];
00238         }
00239     } else if (q->bitrate != SILENCE) {
00240         if (q->bitrate == RATE_OCTAVE) {
00241             g1[0] = 2 * q->frame.cbgain[0] +
00242                     av_clip((q->prev_g1[0] + q->prev_g1[1]) / 2 - 5, 0, 54);
00243             subframes_count = 8;
00244         } else {
00245             assert(q->bitrate == I_F_Q);
00246 
00247             g1[0] = q->prev_g1[1];
00248             switch (q->erasure_count) {
00249             case 1 : break;
00250             case 2 : g1[0] -= 1; break;
00251             case 3 : g1[0] -= 2; break;
00252             default: g1[0] -= 6;
00253             }
00254             if (g1[0] < 0)
00255                 g1[0] = 0;
00256             subframes_count = 4;
00257         }
00258         // This interpolation is done to produce smoother background noise.
00259         slope = 0.5 * (qcelp_g12ga[g1[0]] - q->last_codebook_gain) / subframes_count;
00260         for (i = 1; i <= subframes_count; i++)
00261                 gain[i - 1] = q->last_codebook_gain + slope * i;
00262 
00263         q->last_codebook_gain = gain[i - 2];
00264         q->prev_g1[0]         = q->prev_g1[1];
00265         q->prev_g1[1]         = g1[0];
00266     }
00267 }
00268 
00278 static int codebook_sanity_check_for_rate_quarter(const uint8_t *cbgain)
00279 {
00280     int i, diff, prev_diff = 0;
00281 
00282     for (i = 1; i < 5; i++) {
00283         diff = cbgain[i] - cbgain[i-1];
00284         if (FFABS(diff) > 10)
00285             return -1;
00286         else if (FFABS(diff - prev_diff) > 12)
00287             return -1;
00288         prev_diff = diff;
00289     }
00290     return 0;
00291 }
00292 
00314 static void compute_svector(QCELPContext *q, const float *gain,
00315                             float *cdn_vector)
00316 {
00317     int i, j, k;
00318     uint16_t cbseed, cindex;
00319     float *rnd, tmp_gain, fir_filter_value;
00320 
00321     switch (q->bitrate) {
00322     case RATE_FULL:
00323         for (i = 0; i < 16; i++) {
00324             tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO;
00325             cindex   = -q->frame.cindex[i];
00326             for (j = 0; j < 10; j++)
00327                 *cdn_vector++ = tmp_gain * qcelp_rate_full_codebook[cindex++ & 127];
00328         }
00329         break;
00330     case RATE_HALF:
00331         for (i = 0; i < 4; i++) {
00332             tmp_gain = gain[i] * QCELP_RATE_HALF_CODEBOOK_RATIO;
00333             cindex   = -q->frame.cindex[i];
00334             for (j = 0; j < 40; j++)
00335                 *cdn_vector++ = tmp_gain * qcelp_rate_half_codebook[cindex++ & 127];
00336         }
00337         break;
00338     case RATE_QUARTER:
00339         cbseed = (0x0003 & q->frame.lspv[4]) << 14 |
00340                  (0x003F & q->frame.lspv[3]) <<  8 |
00341                  (0x0060 & q->frame.lspv[2]) <<  1 |
00342                  (0x0007 & q->frame.lspv[1]) <<  3 |
00343                  (0x0038 & q->frame.lspv[0]) >>  3;
00344         rnd    = q->rnd_fir_filter_mem + 20;
00345         for (i = 0; i < 8; i++) {
00346             tmp_gain = gain[i] * (QCELP_SQRT1887 / 32768.0);
00347             for (k = 0; k < 20; k++) {
00348                 cbseed = 521 * cbseed + 259;
00349                 *rnd   = (int16_t) cbseed;
00350 
00351                     // FIR filter
00352                 fir_filter_value = 0.0;
00353                 for (j = 0; j < 10; j++)
00354                     fir_filter_value += qcelp_rnd_fir_coefs[j] *
00355                                         (rnd[-j] + rnd[-20+j]);
00356 
00357                 fir_filter_value += qcelp_rnd_fir_coefs[10] * rnd[-10];
00358                 *cdn_vector++     = tmp_gain * fir_filter_value;
00359                 rnd++;
00360             }
00361         }
00362         memcpy(q->rnd_fir_filter_mem, q->rnd_fir_filter_mem + 160,
00363                20 * sizeof(float));
00364         break;
00365     case RATE_OCTAVE:
00366         cbseed = q->first16bits;
00367         for (i = 0; i < 8; i++) {
00368             tmp_gain = gain[i] * (QCELP_SQRT1887 / 32768.0);
00369             for (j = 0; j < 20; j++) {
00370                 cbseed        = 521 * cbseed + 259;
00371                 *cdn_vector++ = tmp_gain * (int16_t) cbseed;
00372             }
00373         }
00374         break;
00375     case I_F_Q:
00376         cbseed = -44; // random codebook index
00377         for (i = 0; i < 4; i++) {
00378             tmp_gain = gain[i] * QCELP_RATE_FULL_CODEBOOK_RATIO;
00379             for (j = 0; j < 40; j++)
00380                 *cdn_vector++ = tmp_gain * qcelp_rate_full_codebook[cbseed++ & 127];
00381         }
00382         break;
00383     case SILENCE:
00384         memset(cdn_vector, 0, 160 * sizeof(float));
00385         break;
00386     }
00387 }
00388 
00398 static void apply_gain_ctrl(float *v_out, const float *v_ref, const float *v_in)
00399 {
00400     int i;
00401 
00402     for (i = 0; i < 160; i += 40)
00403         ff_scale_vector_to_given_sum_of_squares(v_out + i, v_in + i,
00404                                                 ff_dot_productf(v_ref + i,
00405                                                                 v_ref + i, 40),
00406                                                 40);
00407 }
00408 
00426 static const float *do_pitchfilter(float memory[303], const float v_in[160],
00427                                    const float gain[4], const uint8_t *lag,
00428                                    const uint8_t pfrac[4])
00429 {
00430     int i, j;
00431     float *v_lag, *v_out;
00432     const float *v_len;
00433 
00434     v_out = memory + 143; // Output vector starts at memory[143].
00435 
00436     for (i = 0; i < 4; i++) {
00437         if (gain[i]) {
00438             v_lag = memory + 143 + 40 * i - lag[i];
00439             for (v_len = v_in + 40; v_in < v_len; v_in++) {
00440                 if (pfrac[i]) { // If it is a fractional lag...
00441                     for (j = 0, *v_out = 0.; j < 4; j++)
00442                         *v_out += qcelp_hammsinc_table[j] * (v_lag[j - 4] + v_lag[3 - j]);
00443                 } else
00444                     *v_out = *v_lag;
00445 
00446                 *v_out = *v_in + gain[i] * *v_out;
00447 
00448                 v_lag++;
00449                 v_out++;
00450             }
00451         } else {
00452             memcpy(v_out, v_in, 40 * sizeof(float));
00453             v_in  += 40;
00454             v_out += 40;
00455         }
00456     }
00457 
00458     memmove(memory, memory + 160, 143 * sizeof(float));
00459     return memory + 143;
00460 }
00461 
00469 static void apply_pitch_filters(QCELPContext *q, float *cdn_vector)
00470 {
00471     int i;
00472     const float *v_synthesis_filtered, *v_pre_filtered;
00473 
00474     if (q->bitrate >= RATE_HALF || q->bitrate == SILENCE ||
00475         (q->bitrate == I_F_Q && (q->prev_bitrate >= RATE_HALF))) {
00476 
00477         if (q->bitrate >= RATE_HALF) {
00478             // Compute gain & lag for the whole frame.
00479             for (i = 0; i < 4; i++) {
00480                 q->pitch_gain[i] = q->frame.plag[i] ? (q->frame.pgain[i] + 1) * 0.25 : 0.0;
00481 
00482                 q->pitch_lag[i] = q->frame.plag[i] + 16;
00483             }
00484         } else {
00485             float max_pitch_gain;
00486 
00487             if (q->bitrate == I_F_Q) {
00488                   if (q->erasure_count < 3)
00489                       max_pitch_gain = 0.9 - 0.3 * (q->erasure_count - 1);
00490                   else
00491                       max_pitch_gain = 0.0;
00492             } else {
00493                 assert(q->bitrate == SILENCE);
00494                 max_pitch_gain = 1.0;
00495             }
00496             for (i = 0; i < 4; i++)
00497                 q->pitch_gain[i] = FFMIN(q->pitch_gain[i], max_pitch_gain);
00498 
00499             memset(q->frame.pfrac, 0, sizeof(q->frame.pfrac));
00500         }
00501 
00502         // pitch synthesis filter
00503         v_synthesis_filtered = do_pitchfilter(q->pitch_synthesis_filter_mem,
00504                                               cdn_vector, q->pitch_gain,
00505                                               q->pitch_lag, q->frame.pfrac);
00506 
00507         // pitch prefilter update
00508         for (i = 0; i < 4; i++)
00509             q->pitch_gain[i] = 0.5 * FFMIN(q->pitch_gain[i], 1.0);
00510 
00511         v_pre_filtered       = do_pitchfilter(q->pitch_pre_filter_mem,
00512                                               v_synthesis_filtered,
00513                                               q->pitch_gain, q->pitch_lag,
00514                                               q->frame.pfrac);
00515 
00516         apply_gain_ctrl(cdn_vector, v_synthesis_filtered, v_pre_filtered);
00517     } else {
00518         memcpy(q->pitch_synthesis_filter_mem, cdn_vector + 17, 143 * sizeof(float));
00519         memcpy(q->pitch_pre_filter_mem, cdn_vector + 17, 143 * sizeof(float));
00520         memset(q->pitch_gain, 0, sizeof(q->pitch_gain));
00521         memset(q->pitch_lag,  0, sizeof(q->pitch_lag));
00522     }
00523 }
00524 
00537 static void lspf2lpc(const float *lspf, float *lpc)
00538 {
00539     double lsp[10];
00540     double bandwidth_expansion_coeff = QCELP_BANDWIDTH_EXPANSION_COEFF;
00541     int i;
00542 
00543     for (i = 0; i < 10; i++)
00544         lsp[i] = cos(M_PI * lspf[i]);
00545 
00546     ff_acelp_lspd2lpc(lsp, lpc, 5);
00547 
00548     for (i = 0; i < 10; i++) {
00549         lpc[i]                    *= bandwidth_expansion_coeff;
00550         bandwidth_expansion_coeff *= QCELP_BANDWIDTH_EXPANSION_COEFF;
00551     }
00552 }
00553 
00565 static void interpolate_lpc(QCELPContext *q, const float *curr_lspf,
00566                             float *lpc, const int subframe_num)
00567 {
00568     float interpolated_lspf[10];
00569     float weight;
00570 
00571     if (q->bitrate >= RATE_QUARTER)
00572         weight = 0.25 * (subframe_num + 1);
00573     else if (q->bitrate == RATE_OCTAVE && !subframe_num)
00574         weight = 0.625;
00575     else
00576         weight = 1.0;
00577 
00578     if (weight != 1.0) {
00579         ff_weighted_vector_sumf(interpolated_lspf, curr_lspf, q->prev_lspf,
00580                                 weight, 1.0 - weight, 10);
00581         lspf2lpc(interpolated_lspf, lpc);
00582     } else if (q->bitrate >= RATE_QUARTER ||
00583                (q->bitrate == I_F_Q && !subframe_num))
00584         lspf2lpc(curr_lspf, lpc);
00585     else if (q->bitrate == SILENCE && !subframe_num)
00586         lspf2lpc(q->prev_lspf, lpc);
00587 }
00588 
00589 static qcelp_packet_rate buf_size2bitrate(const int buf_size)
00590 {
00591     switch (buf_size) {
00592     case 35: return RATE_FULL;
00593     case 17: return RATE_HALF;
00594     case  8: return RATE_QUARTER;
00595     case  4: return RATE_OCTAVE;
00596     case  1: return SILENCE;
00597     }
00598 
00599     return I_F_Q;
00600 }
00601 
00614 static qcelp_packet_rate determine_bitrate(AVCodecContext *avctx,
00615                                            const int buf_size,
00616                                            const uint8_t **buf)
00617 {
00618     qcelp_packet_rate bitrate;
00619 
00620     if ((bitrate = buf_size2bitrate(buf_size)) >= 0) {
00621         if (bitrate > **buf) {
00622             QCELPContext *q = avctx->priv_data;
00623             if (!q->warned_buf_mismatch_bitrate) {
00624             av_log(avctx, AV_LOG_WARNING,
00625                    "Claimed bitrate and buffer size mismatch.\n");
00626                 q->warned_buf_mismatch_bitrate = 1;
00627             }
00628             bitrate = **buf;
00629         } else if (bitrate < **buf) {
00630             av_log(avctx, AV_LOG_ERROR,
00631                    "Buffer is too small for the claimed bitrate.\n");
00632             return I_F_Q;
00633         }
00634         (*buf)++;
00635     } else if ((bitrate = buf_size2bitrate(buf_size + 1)) >= 0) {
00636         av_log(avctx, AV_LOG_WARNING,
00637                "Bitrate byte is missing, guessing the bitrate from packet size.\n");
00638     } else
00639         return I_F_Q;
00640 
00641     if (bitrate == SILENCE) {
00642         //FIXME: Remove experimental warning when tested with samples.
00643         av_log_ask_for_sample(avctx, "'Blank frame handling is experimental.");
00644     }
00645     return bitrate;
00646 }
00647 
00648 static void warn_insufficient_frame_quality(AVCodecContext *avctx,
00649                                             const char *message)
00650 {
00651     av_log(avctx, AV_LOG_WARNING, "Frame #%d, IFQ: %s\n",
00652            avctx->frame_number, message);
00653 }
00654 
00655 static void postfilter(QCELPContext *q, float *samples, float *lpc)
00656 {
00657     static const float pow_0_775[10] = {
00658         0.775000, 0.600625, 0.465484, 0.360750, 0.279582,
00659         0.216676, 0.167924, 0.130141, 0.100859, 0.078166
00660     }, pow_0_625[10] = {
00661         0.625000, 0.390625, 0.244141, 0.152588, 0.095367,
00662         0.059605, 0.037253, 0.023283, 0.014552, 0.009095
00663     };
00664     float lpc_s[10], lpc_p[10], pole_out[170], zero_out[160];
00665     int n;
00666 
00667     for (n = 0; n < 10; n++) {
00668         lpc_s[n] = lpc[n] * pow_0_625[n];
00669         lpc_p[n] = lpc[n] * pow_0_775[n];
00670     }
00671 
00672     ff_celp_lp_zero_synthesis_filterf(zero_out, lpc_s,
00673                                       q->formant_mem + 10, 160, 10);
00674     memcpy(pole_out, q->postfilter_synth_mem, sizeof(float) * 10);
00675     ff_celp_lp_synthesis_filterf(pole_out + 10, lpc_p, zero_out, 160, 10);
00676     memcpy(q->postfilter_synth_mem, pole_out + 160, sizeof(float) * 10);
00677 
00678     ff_tilt_compensation(&q->postfilter_tilt_mem, 0.3, pole_out + 10, 160);
00679 
00680     ff_adaptive_gain_control(samples, pole_out + 10,
00681                              ff_dot_productf(q->formant_mem + 10,
00682                                              q->formant_mem + 10, 160),
00683                              160, 0.9375, &q->postfilter_agc_mem);
00684 }
00685 
00686 static int qcelp_decode_frame(AVCodecContext *avctx, void *data,
00687                               int *got_frame_ptr, AVPacket *avpkt)
00688 {
00689     const uint8_t *buf = avpkt->data;
00690     int buf_size       = avpkt->size;
00691     QCELPContext *q    = avctx->priv_data;
00692     float *outbuffer;
00693     int   i, ret;
00694     float quantized_lspf[10], lpc[10];
00695     float gain[16];
00696     float *formant_mem;
00697 
00698     /* get output buffer */
00699     q->avframe.nb_samples = 160;
00700     if ((ret = avctx->get_buffer(avctx, &q->avframe)) < 0) {
00701         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00702         return ret;
00703     }
00704     outbuffer = (float *)q->avframe.data[0];
00705 
00706     if ((q->bitrate = determine_bitrate(avctx, buf_size, &buf)) == I_F_Q) {
00707         warn_insufficient_frame_quality(avctx, "bitrate cannot be determined.");
00708         goto erasure;
00709     }
00710 
00711     if (q->bitrate == RATE_OCTAVE &&
00712         (q->first16bits = AV_RB16(buf)) == 0xFFFF) {
00713         warn_insufficient_frame_quality(avctx, "Bitrate is 1/8 and first 16 bits are on.");
00714         goto erasure;
00715     }
00716 
00717     if (q->bitrate > SILENCE) {
00718         const QCELPBitmap *bitmaps     = qcelp_unpacking_bitmaps_per_rate[q->bitrate];
00719         const QCELPBitmap *bitmaps_end = qcelp_unpacking_bitmaps_per_rate[q->bitrate] +
00720                                          qcelp_unpacking_bitmaps_lengths[q->bitrate];
00721         uint8_t *unpacked_data         = (uint8_t *)&q->frame;
00722 
00723         init_get_bits(&q->gb, buf, 8 * buf_size);
00724 
00725         memset(&q->frame, 0, sizeof(QCELPFrame));
00726 
00727         for (; bitmaps < bitmaps_end; bitmaps++)
00728             unpacked_data[bitmaps->index] |= get_bits(&q->gb, bitmaps->bitlen) << bitmaps->bitpos;
00729 
00730         // Check for erasures/blanks on rates 1, 1/4 and 1/8.
00731         if (q->frame.reserved) {
00732             warn_insufficient_frame_quality(avctx, "Wrong data in reserved frame area.");
00733             goto erasure;
00734         }
00735         if (q->bitrate == RATE_QUARTER &&
00736             codebook_sanity_check_for_rate_quarter(q->frame.cbgain)) {
00737             warn_insufficient_frame_quality(avctx, "Codebook gain sanity check failed.");
00738             goto erasure;
00739         }
00740 
00741         if (q->bitrate >= RATE_HALF) {
00742             for (i = 0; i < 4; i++) {
00743                 if (q->frame.pfrac[i] && q->frame.plag[i] >= 124) {
00744                     warn_insufficient_frame_quality(avctx, "Cannot initialize pitch filter.");
00745                     goto erasure;
00746                 }
00747             }
00748         }
00749     }
00750 
00751     decode_gain_and_index(q, gain);
00752     compute_svector(q, gain, outbuffer);
00753 
00754     if (decode_lspf(q, quantized_lspf) < 0) {
00755         warn_insufficient_frame_quality(avctx, "Badly received packets in frame.");
00756         goto erasure;
00757     }
00758 
00759     apply_pitch_filters(q, outbuffer);
00760 
00761     if (q->bitrate == I_F_Q) {
00762 erasure:
00763         q->bitrate = I_F_Q;
00764         q->erasure_count++;
00765         decode_gain_and_index(q, gain);
00766         compute_svector(q, gain, outbuffer);
00767         decode_lspf(q, quantized_lspf);
00768         apply_pitch_filters(q, outbuffer);
00769     } else
00770         q->erasure_count = 0;
00771 
00772     formant_mem = q->formant_mem + 10;
00773     for (i = 0; i < 4; i++) {
00774         interpolate_lpc(q, quantized_lspf, lpc, i);
00775         ff_celp_lp_synthesis_filterf(formant_mem, lpc, outbuffer + i * 40, 40, 10);
00776         formant_mem += 40;
00777     }
00778 
00779     // postfilter, as per TIA/EIA/IS-733 2.4.8.6
00780     postfilter(q, outbuffer, lpc);
00781 
00782     memcpy(q->formant_mem, q->formant_mem + 160, 10 * sizeof(float));
00783 
00784     memcpy(q->prev_lspf, quantized_lspf, sizeof(q->prev_lspf));
00785     q->prev_bitrate  = q->bitrate;
00786 
00787     *got_frame_ptr   = 1;
00788     *(AVFrame *)data = q->avframe;
00789 
00790     return buf_size;
00791 }
00792 
00793 AVCodec ff_qcelp_decoder = {
00794     .name           = "qcelp",
00795     .type           = AVMEDIA_TYPE_AUDIO,
00796     .id             = CODEC_ID_QCELP,
00797     .init           = qcelp_decode_init,
00798     .decode         = qcelp_decode_frame,
00799     .capabilities   = CODEC_CAP_DR1,
00800     .priv_data_size = sizeof(QCELPContext),
00801     .long_name      = NULL_IF_CONFIG_SMALL("QCELP / PureVoice"),
00802 };
Generated on Sun Apr 22 2012 21:54:03 for Libav by doxygen 1.7.1