📄 decoder.c
字号:
#ifdef LTP_DEC if (hDecoder->lt_pred_stat[i]) faad_free(hDecoder->lt_pred_stat[i]);#endif }#ifdef SSR_DEC if (hDecoder->object_type == SSR) ssr_filter_bank_end(hDecoder->fb); else#endif filter_bank_end(hDecoder->fb); drc_end(hDecoder->drc); if (hDecoder->sample_buffer) faad_free(hDecoder->sample_buffer);#ifdef SBR_DEC for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++) { if (hDecoder->sbr[i]) sbrDecodeEnd(hDecoder->sbr[i]); }#endif if (hDecoder) faad_free(hDecoder);}void FAADAPI faacDecPostSeekReset(faacDecHandle hDecoder, int32_t frame){ if (hDecoder) { hDecoder->postSeekResetFlag = 1; if (frame != -1) hDecoder->frame = frame; }}static void create_channel_config(faacDecHandle hDecoder, faacDecFrameInfo *hInfo){ hInfo->num_front_channels = 0; hInfo->num_side_channels = 0; hInfo->num_back_channels = 0; hInfo->num_lfe_channels = 0; memset(hInfo->channel_position, 0, MAX_CHANNELS*sizeof(uint8_t)); if (hDecoder->downMatrix) { hInfo->num_front_channels = 2; hInfo->channel_position[0] = FRONT_CHANNEL_LEFT; hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT; return; } /* check if there is a PCE */ if (hDecoder->pce_set) { uint8_t i, chpos = 0; uint8_t chdir, back_center = 0; hInfo->num_front_channels = hDecoder->pce.num_front_channels; hInfo->num_side_channels = hDecoder->pce.num_side_channels; hInfo->num_back_channels = hDecoder->pce.num_back_channels; hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels; chdir = hInfo->num_front_channels; if (chdir & 1) { hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER; chdir--; } for (i = 0; i < chdir; i += 2) { hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT; hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT; } for (i = 0; i < hInfo->num_side_channels; i += 2) { hInfo->channel_position[chpos++] = SIDE_CHANNEL_LEFT; hInfo->channel_position[chpos++] = SIDE_CHANNEL_RIGHT; } chdir = hInfo->num_back_channels; if (chdir & 1) { back_center = 1; chdir--; } for (i = 0; i < chdir; i += 2) { hInfo->channel_position[chpos++] = BACK_CHANNEL_LEFT; hInfo->channel_position[chpos++] = BACK_CHANNEL_RIGHT; } if (back_center) { hInfo->channel_position[chpos++] = BACK_CHANNEL_CENTER; } for (i = 0; i < hInfo->num_lfe_channels; i++) { hInfo->channel_position[chpos++] = LFE_CHANNEL; } } else { switch (hDecoder->channelConfiguration) { case 1: hInfo->num_front_channels = 1; hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; break; case 2: hInfo->num_front_channels = 2; hInfo->channel_position[0] = FRONT_CHANNEL_LEFT; hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT; break; case 3: hInfo->num_front_channels = 3; hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; hInfo->channel_position[1] = FRONT_CHANNEL_LEFT; hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT; break; case 4: hInfo->num_front_channels = 3; hInfo->num_back_channels = 1; hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; hInfo->channel_position[1] = FRONT_CHANNEL_LEFT; hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT; hInfo->channel_position[3] = BACK_CHANNEL_CENTER; break; case 5: hInfo->num_front_channels = 3; hInfo->num_back_channels = 2; hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; hInfo->channel_position[1] = FRONT_CHANNEL_LEFT; hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT; hInfo->channel_position[3] = BACK_CHANNEL_LEFT; hInfo->channel_position[4] = BACK_CHANNEL_RIGHT; break; case 6: hInfo->num_front_channels = 3; hInfo->num_back_channels = 2; hInfo->num_lfe_channels = 1; hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; hInfo->channel_position[1] = FRONT_CHANNEL_LEFT; hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT; hInfo->channel_position[3] = BACK_CHANNEL_LEFT; hInfo->channel_position[4] = BACK_CHANNEL_RIGHT; hInfo->channel_position[5] = LFE_CHANNEL; break; case 7: hInfo->num_front_channels = 3; hInfo->num_side_channels = 2; hInfo->num_back_channels = 2; hInfo->num_lfe_channels = 1; hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; hInfo->channel_position[1] = FRONT_CHANNEL_LEFT; hInfo->channel_position[2] = FRONT_CHANNEL_RIGHT; hInfo->channel_position[3] = SIDE_CHANNEL_LEFT; hInfo->channel_position[4] = SIDE_CHANNEL_RIGHT; hInfo->channel_position[5] = BACK_CHANNEL_LEFT; hInfo->channel_position[6] = BACK_CHANNEL_RIGHT; hInfo->channel_position[7] = LFE_CHANNEL; break; default: /* channelConfiguration == 0 || channelConfiguration > 7 */ { uint8_t i; uint8_t ch = hDecoder->fr_channels - hDecoder->has_lfe; if (ch & 1) /* there's either a center front or a center back channel */ { uint8_t ch1 = (ch-1)/2; if (hDecoder->first_syn_ele == ID_SCE) { hInfo->num_front_channels = ch1 + 1; hInfo->num_back_channels = ch1; hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; for (i = 1; i <= ch1; i+=2) { hInfo->channel_position[i] = FRONT_CHANNEL_LEFT; hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT; } for (i = ch1+1; i < ch; i+=2) { hInfo->channel_position[i] = BACK_CHANNEL_LEFT; hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT; } } else { hInfo->num_front_channels = ch1; hInfo->num_back_channels = ch1 + 1; for (i = 0; i < ch1; i+=2) { hInfo->channel_position[i] = FRONT_CHANNEL_LEFT; hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT; } for (i = ch1; i < ch-1; i+=2) { hInfo->channel_position[i] = BACK_CHANNEL_LEFT; hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT; } hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER; } } else { uint8_t ch1 = (ch)/2; hInfo->num_front_channels = ch1; hInfo->num_back_channels = ch1; if (ch1 & 1) { hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; for (i = 1; i <= ch1; i+=2) { hInfo->channel_position[i] = FRONT_CHANNEL_LEFT; hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT; } for (i = ch1+1; i < ch-1; i+=2) { hInfo->channel_position[i] = BACK_CHANNEL_LEFT; hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT; } hInfo->channel_position[ch-1] = BACK_CHANNEL_CENTER; } else { for (i = 0; i < ch1; i+=2) { hInfo->channel_position[i] = FRONT_CHANNEL_LEFT; hInfo->channel_position[i+1] = FRONT_CHANNEL_RIGHT; } for (i = ch1; i < ch; i+=2) { hInfo->channel_position[i] = BACK_CHANNEL_LEFT; hInfo->channel_position[i+1] = BACK_CHANNEL_RIGHT; } } } hInfo->num_lfe_channels = hDecoder->has_lfe; for (i = ch; i < hDecoder->fr_channels; i++) { hInfo->channel_position[i] = LFE_CHANNEL; } } break; } }}void* FAADAPI faacDecDecode(faacDecHandle hDecoder, faacDecFrameInfo *hInfo, uint8_t *buffer, uint32_t buffer_size){ uint8_t channels = 0; uint8_t output_channels = 0; bitfile ld; uint32_t bitsconsumed; uint16_t frame_len; void *sample_buffer;#ifdef PROFILE int64_t count = faad_get_ts();#endif /* safety checks */ if ((hDecoder == NULL) || (hInfo == NULL) || (buffer == NULL)) { return NULL; } frame_len = hDecoder->frameLength; memset(hInfo, 0, sizeof(faacDecFrameInfo)); memset(hDecoder->internal_channel, 0, MAX_CHANNELS*sizeof(hDecoder->internal_channel[0])); /* initialize the bitstream */ faad_initbits(&ld, buffer, buffer_size);#ifdef DRM if (hDecoder->object_type == DRM_ER_LC) { /* We do not support stereo right now */ if (hDecoder->channelConfiguration == 2) { hInfo->error = 8; // Throw CRC error goto error; } faad_getbits(&ld, 8 DEBUGVAR(1,1,"faacDecDecode(): skip CRC")); }#endif if (hDecoder->adts_header_present) { adts_header adts; adts.old_format = hDecoder->config.useOldADTSFormat; if ((hInfo->error = adts_frame(&adts, &ld)) > 0) goto error; /* MPEG2 does byte_alignment() here, * but ADTS header is always multiple of 8 bits in MPEG2 * so not needed to actually do it. */ }#ifdef ANALYSIS dbg_count = 0;#endif /* decode the complete bitstream */#ifdef SCALABLE_DEC if ((hDecoder->object_type == 6) || (hDecoder->object_type == DRM_ER_LC)) { aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc); } else {#endif raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc);#ifdef SCALABLE_DEC }#endif channels = hDecoder->fr_channels; if (hInfo->error > 0) goto error; /* safety check */ if (channels == 0 || channels > MAX_CHANNELS) { /* invalid number of channels */ hInfo->error = 12; goto error; } /* no more bit reading after this */ bitsconsumed = faad_get_processed_bits(&ld); hInfo->bytesconsumed = bit2byte(bitsconsumed); if (ld.error) { hInfo->error = 14; goto error; } faad_endbits(&ld); if (!hDecoder->adts_header_present && !hDecoder->adif_header_present) { if (channels != hDecoder->channelConfiguration) hDecoder->channelConfiguration = channels; if (channels == 8) /* 7.1 */ hDecoder->channelConfiguration = 7; if (channels == 7) /* not a standard channelConfiguration */ hDecoder->channelConfiguration = 0; } if ((channels == 5 || channels == 6) && hDecoder->config.downMatrix) { hDecoder->downMatrix = 1; output_channels = 2; } else { output_channels = channels; } /* Make a channel configuration based on either a PCE or a channelConfiguration */ create_channel_config(hDecoder, hInfo); /* number of samples in this frame */ hInfo->samples = frame_len*output_channels; /* number of channels in this frame */ hInfo->channels = output_channels; /* samplerate */ hInfo->samplerate = get_sample_rate(hDecoder->sf_index); /* object type */ hInfo->object_type = hDecoder->object_type; /* sbr */ hInfo->sbr = NO_SBR; /* header type */ hInfo->header_type = RAW; if (hDecoder->adif_header_present) hInfo->header_type = ADIF; if (hDecoder->adts_header_present) hInfo->header_type = ADTS; /* check if frame has channel elements */ if (channels == 0) { hDecoder->frame++; return NULL; } /* allocate the buffer for the final samples */ if ((hDecoder->sample_buffer == NULL) || (hDecoder->alloced_channels != output_channels)) { static const uint8_t str[] = { sizeof(int16_t), sizeof(int32_t), sizeof(int32_t), sizeof(float32_t), sizeof(double), sizeof(int16_t), sizeof(int16_t), sizeof(int16_t), sizeof(int16_t), 0, 0, 0 }; uint8_t stride = str[hDecoder->config.outputFormat-1];#ifdef SBR_DEC if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1)) stride = 2 * stride;#endif if (hDecoder->sample_buffer) faad_free(hDecoder->sample_buffer); hDecoder->sample_buffer = NULL; hDecoder->sample_buffer = faad_malloc(frame_len*output_channels*stride); hDecoder->alloced_channels = output_channels; } sample_buffer = hDecoder->sample_buffer;#ifdef SBR_DEC if ((hDecoder->sbr_present_flag == 1) || (hDecoder->forceUpSampling == 1)) { uint8_t ele; /* this data is different when SBR is used or when the data is upsampled */ frame_len *= 2; hInfo->samples *= 2; hInfo->samplerate *= 2; /* check if every element was provided with SBR data */ for (ele = 0; ele < hDecoder->fr_ch_ele; ele++) { if (hDecoder->sbr[ele] == NULL) { hInfo->error = 25; goto error; } } /* sbr */ if (hDecoder->sbr_present_flag == 1) { hInfo->object_type = HE_AAC; hInfo->sbr = SBR_UPSAMPLED; } else { hInfo->sbr = NO_SBR_UPSAMPLED; } }#endif sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer, output_channels, frame_len, hDecoder->config.outputFormat); hDecoder->postSeekResetFlag = 0; hDecoder->frame++;#ifdef LD_DEC if (hDecoder->object_type != LD) {#endif if (hDecoder->frame <= 1) hInfo->samples = 0;#ifdef LD_DEC } else { /* LD encoders will give lower delay */ if (hDecoder->frame <= 0) hInfo->samples = 0; }#endif /* cleanup */#ifdef ANALYSIS fflush(stdout);#endif#ifdef PROFILE count = faad_get_ts() - count; hDecoder->cycles += count;#endif return sample_buffer;error: faad_endbits(&ld); /* cleanup */#ifdef ANALYSIS fflush(stdout);#endif return NULL;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -