Mercurial > audlegacy-plugins
comparison src/aac/libfaad2/decoder.c @ 681:1d8b08df98c3 trunk
[svn] - synchronise with FAAD 2.5
| author | nenolod |
|---|---|
| date | Tue, 20 Feb 2007 04:37:37 -0800 |
| parents | 3da1b8942b8b |
| children | e6c5fdae6e88 |
comparison
equal
deleted
inserted
replaced
| 680:3a06665a810e | 681:1d8b08df98c3 |
|---|---|
| 1 /* | 1 /* |
| 2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding | 2 ** FAAD2 - Freeware Advanced Audio (AAC) Decoder including SBR decoding |
| 3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software AG, http://www.nero.com | 3 ** Copyright (C) 2003-2005 M. Bakker, Nero AG, http://www.nero.com |
| 4 ** | 4 ** |
| 5 ** This program is free software; you can redistribute it and/or modify | 5 ** This program is free software; you can redistribute it and/or modify |
| 6 ** it under the terms of the GNU General Public License as published by | 6 ** it under the terms of the GNU General Public License as published by |
| 7 ** the Free Software Foundation; either version 2 of the License, or | 7 ** the Free Software Foundation; either version 2 of the License, or |
| 8 ** (at your option) any later version. | 8 ** (at your option) any later version. |
| 9 ** | 9 ** |
| 10 ** This program is distributed in the hope that it will be useful, | 10 ** This program is distributed in the hope that it will be useful, |
| 11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 ** GNU General Public License for more details. | 13 ** GNU General Public License for more details. |
| 14 ** | 14 ** |
| 15 ** You should have received a copy of the GNU General Public License | 15 ** You should have received a copy of the GNU General Public License |
| 16 ** along with this program; if not, write to the Free Software | 16 ** along with this program; if not, write to the Free Software |
| 17 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | 17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 18 ** | 18 ** |
| 19 ** Any non-GPL usage of this software or parts of this software is strictly | 19 ** Any non-GPL usage of this software or parts of this software is strictly |
| 20 ** forbidden. | 20 ** forbidden. |
| 21 ** | 21 ** |
| 22 ** Software using this code must display the following message visibly in or | |
| 23 ** on each copy of the software: | |
| 24 ** "FAAD2 AAC/HE-AAC/HE-AACv2/DRM decoder (c) Nero AG, www.nero.com" | |
| 25 ** in, for example, the about-box or help/startup screen. | |
| 26 ** | |
| 22 ** Commercial non-GPL licensing of this software is possible. | 27 ** Commercial non-GPL licensing of this software is possible. |
| 23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com. | 28 ** For more info contact Nero AG through Mpeg4AAClicense@nero.com. |
| 24 ** | 29 ** |
| 25 ** $Id: decoder.c,v 1.107 2004/09/08 09:43:11 gcp Exp $ | 30 ** $Id: decoder.c,v 1.109 2006/05/07 18:09:00 menno Exp $ |
| 26 **/ | 31 **/ |
| 27 | 32 |
| 28 #include "common.h" | 33 #include "common.h" |
| 29 #include "structs.h" | 34 #include "structs.h" |
| 30 | 35 |
| 114 hDecoder->frameLength = 1024; | 119 hDecoder->frameLength = 1024; |
| 115 | 120 |
| 116 hDecoder->frame = 0; | 121 hDecoder->frame = 0; |
| 117 hDecoder->sample_buffer = NULL; | 122 hDecoder->sample_buffer = NULL; |
| 118 | 123 |
| 124 hDecoder->__r1 = 1; | |
| 125 hDecoder->__r2 = 1; | |
| 126 | |
| 119 for (i = 0; i < MAX_CHANNELS; i++) | 127 for (i = 0; i < MAX_CHANNELS; i++) |
| 120 { | 128 { |
| 121 hDecoder->window_shape_prev[i] = 0; | 129 hDecoder->window_shape_prev[i] = 0; |
| 122 hDecoder->time_out[i] = NULL; | 130 hDecoder->time_out[i] = NULL; |
| 123 hDecoder->fb_intermed[i] = NULL; | 131 hDecoder->fb_intermed[i] = NULL; |
| 192 } | 200 } |
| 193 | 201 |
| 194 return 0; | 202 return 0; |
| 195 } | 203 } |
| 196 | 204 |
| 205 | |
| 197 int32_t NEAACDECAPI NeAACDecInit(NeAACDecHandle hDecoder, uint8_t *buffer, | 206 int32_t NEAACDECAPI NeAACDecInit(NeAACDecHandle hDecoder, uint8_t *buffer, |
| 198 uint32_t buffer_size, | 207 uint32_t buffer_size, |
| 199 uint32_t *samplerate, uint8_t *channels) | 208 uint32_t *samplerate, uint8_t *channels) |
| 200 { | 209 { |
| 201 uint32_t bits = 0; | 210 uint32_t bits = 0; |
| 202 bitfile ld; | 211 bitfile ld; |
| 203 adif_header adif; | 212 adif_header adif; |
| 204 adts_header adts; | 213 adts_header adts; |
| 205 | 214 |
| 215 | |
| 206 if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL)) | 216 if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL)) |
| 207 return -1; | 217 return -1; |
| 208 | 218 |
| 209 hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate); | 219 hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate); |
| 210 hDecoder->object_type = hDecoder->config.defObjectType; | 220 hDecoder->object_type = hDecoder->config.defObjectType; |
| 255 faad_endbits(&ld); | 265 faad_endbits(&ld); |
| 256 return -1; | 266 return -1; |
| 257 } | 267 } |
| 258 faad_endbits(&ld); | 268 faad_endbits(&ld); |
| 259 } | 269 } |
| 260 hDecoder->channelConfiguration = *channels; | |
| 261 | 270 |
| 262 #if (defined(PS_DEC) || defined(DRM_PS)) | 271 #if (defined(PS_DEC) || defined(DRM_PS)) |
| 263 /* check if we have a mono file */ | 272 /* check if we have a mono file */ |
| 264 if (*channels == 1) | 273 if (*channels == 1) |
| 265 { | 274 { |
| 266 /* upMatrix to 2 channels for implicit signalling of PS */ | 275 /* upMatrix to 2 channels for implicit signalling of PS */ |
| 267 *channels = 2; | 276 *channels = 2; |
| 268 } | 277 } |
| 269 #endif | 278 #endif |
| 279 | |
| 280 hDecoder->channelConfiguration = *channels; | |
| 270 | 281 |
| 271 #ifdef SBR_DEC | 282 #ifdef SBR_DEC |
| 272 /* implicit signalling */ | 283 /* implicit signalling */ |
| 273 if (*samplerate <= 24000 && !(hDecoder->config.dontUpSampleImplicitSBR)) | 284 if (*samplerate <= 24000 && !(hDecoder->config.dontUpSampleImplicitSBR)) |
| 274 { | 285 { |
| 420 | 431 |
| 421 #ifdef SBR_DEC | 432 #ifdef SBR_DEC |
| 422 if ((channels == DRMCH_MONO) || (channels == DRMCH_STEREO)) | 433 if ((channels == DRMCH_MONO) || (channels == DRMCH_STEREO)) |
| 423 (*hDecoder)->sbr_present_flag = 0; | 434 (*hDecoder)->sbr_present_flag = 0; |
| 424 else | 435 else |
| 425 (*hDecoder)->sbr_present_flag = 1; | 436 (*hDecoder)->sbr_present_flag = 1; |
| 426 #endif | 437 #endif |
| 427 | 438 |
| 428 (*hDecoder)->fb = filter_bank_init((*hDecoder)->frameLength); | 439 (*hDecoder)->fb = filter_bank_init((*hDecoder)->frameLength); |
| 429 | 440 |
| 430 return 0; | 441 return 0; |
| 431 } | 442 } |
| 523 hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels; | 534 hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels; |
| 524 | 535 |
| 525 chdir = hInfo->num_front_channels; | 536 chdir = hInfo->num_front_channels; |
| 526 if (chdir & 1) | 537 if (chdir & 1) |
| 527 { | 538 { |
| 539 #if (defined(PS_DEC) || defined(DRM_PS)) | |
| 540 /* When PS is enabled output is always stereo */ | |
| 541 hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT; | |
| 542 hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT; | |
| 543 #else | |
| 528 hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER; | 544 hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER; |
| 529 chdir--; | 545 chdir--; |
| 546 #endif | |
| 530 } | 547 } |
| 531 for (i = 0; i < chdir; i += 2) | 548 for (i = 0; i < chdir; i += 2) |
| 532 { | 549 { |
| 533 hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT; | 550 hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT; |
| 534 hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT; | 551 hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT; |
| 563 | 580 |
| 564 } else { | 581 } else { |
| 565 switch (hDecoder->channelConfiguration) | 582 switch (hDecoder->channelConfiguration) |
| 566 { | 583 { |
| 567 case 1: | 584 case 1: |
| 585 #if (defined(PS_DEC) || defined(DRM_PS)) | |
| 586 /* When PS is enabled output is always stereo */ | |
| 587 hInfo->num_front_channels = 2; | |
| 588 hInfo->channel_position[0] = FRONT_CHANNEL_LEFT; | |
| 589 hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT; | |
| 590 #else | |
| 568 hInfo->num_front_channels = 1; | 591 hInfo->num_front_channels = 1; |
| 569 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; | 592 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; |
| 593 #endif | |
| 570 break; | 594 break; |
| 571 case 2: | 595 case 2: |
| 572 hInfo->num_front_channels = 2; | 596 hInfo->num_front_channels = 2; |
| 573 hInfo->channel_position[0] = FRONT_CHANNEL_LEFT; | 597 hInfo->channel_position[0] = FRONT_CHANNEL_LEFT; |
| 574 hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT; | 598 hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT; |
| 720 | 744 |
| 721 return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size, | 745 return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size, |
| 722 sample_buffer, sample_buffer_size); | 746 sample_buffer, sample_buffer_size); |
| 723 } | 747 } |
| 724 | 748 |
| 749 #ifdef DRM | |
| 750 | |
| 751 #define ERROR_STATE_INIT 6 | |
| 752 | |
| 753 static void conceal_output(NeAACDecHandle hDecoder, uint16_t frame_len, | |
| 754 uint8_t out_ch, void *sample_buffer) | |
| 755 { | |
| 756 return; | |
| 757 } | |
| 758 #endif | |
| 759 | |
| 725 static void* aac_frame_decode(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo, | 760 static void* aac_frame_decode(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo, |
| 726 uint8_t *buffer, uint32_t buffer_size, | 761 uint8_t *buffer, uint32_t buffer_size, |
| 727 void **sample_buffer2, uint32_t sample_buffer_size) | 762 void **sample_buffer2, uint32_t sample_buffer_size) |
| 728 { | 763 { |
| 764 uint16_t i; | |
| 729 uint8_t channels = 0; | 765 uint8_t channels = 0; |
| 730 uint8_t output_channels = 0; | 766 uint8_t output_channels = 0; |
| 731 bitfile ld; | 767 bitfile ld = {0}; |
| 732 uint32_t bitsconsumed; | 768 uint32_t bitsconsumed; |
| 733 uint16_t frame_len; | 769 uint16_t frame_len; |
| 734 void *sample_buffer; | 770 void *sample_buffer; |
| 735 | 771 |
| 736 #ifdef PROFILE | 772 #ifdef PROFILE |
| 750 frame_len = hDecoder->frameLength; | 786 frame_len = hDecoder->frameLength; |
| 751 | 787 |
| 752 | 788 |
| 753 memset(hInfo, 0, sizeof(NeAACDecFrameInfo)); | 789 memset(hInfo, 0, sizeof(NeAACDecFrameInfo)); |
| 754 memset(hDecoder->internal_channel, 0, MAX_CHANNELS*sizeof(hDecoder->internal_channel[0])); | 790 memset(hDecoder->internal_channel, 0, MAX_CHANNELS*sizeof(hDecoder->internal_channel[0])); |
| 791 | |
| 792 #ifdef USE_TIME_LIMIT | |
| 793 if ((TIME_LIMIT * get_sample_rate(hDecoder->sf_index)) > hDecoder->TL_count) | |
| 794 { | |
| 795 hDecoder->TL_count += 1024; | |
| 796 } else { | |
| 797 hInfo->error = (NUM_ERROR_MESSAGES-1); | |
| 798 goto error; | |
| 799 } | |
| 800 #endif | |
| 801 | |
| 802 | |
| 803 /* check for some common metadata tag types in the bitstream | |
| 804 * No need to return an error | |
| 805 */ | |
| 806 /* ID3 */ | |
| 807 if (buffer_size >= 128) | |
| 808 { | |
| 809 if (memcmp(buffer, "TAG", 3) == 0) | |
| 810 { | |
| 811 /* found it */ | |
| 812 hInfo->bytesconsumed = 128; /* 128 bytes fixed size */ | |
| 813 /* no error, but no output either */ | |
| 814 return NULL; | |
| 815 } | |
| 816 } | |
| 817 | |
| 755 | 818 |
| 756 /* initialize the bitstream */ | 819 /* initialize the bitstream */ |
| 757 faad_initbits(&ld, buffer, buffer_size); | 820 faad_initbits(&ld, buffer, buffer_size); |
| 758 | 821 |
| 759 #if 0 | 822 #if 0 |
| 778 if (hDecoder->object_type == DRM_ER_LC) | 841 if (hDecoder->object_type == DRM_ER_LC) |
| 779 { | 842 { |
| 780 /* We do not support stereo right now */ | 843 /* We do not support stereo right now */ |
| 781 if (0) //(hDecoder->channelConfiguration == 2) | 844 if (0) //(hDecoder->channelConfiguration == 2) |
| 782 { | 845 { |
| 783 hInfo->error = 8; // Throw CRC error | 846 hInfo->error = 28; // Throw CRC error |
| 784 goto error; | 847 goto error; |
| 785 } | 848 } |
| 786 | 849 |
| 787 faad_getbits(&ld, 8 | 850 faad_getbits(&ld, 8 |
| 788 DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC")); | 851 DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC")); |
| 806 #ifdef ANALYSIS | 869 #ifdef ANALYSIS |
| 807 dbg_count = 0; | 870 dbg_count = 0; |
| 808 #endif | 871 #endif |
| 809 | 872 |
| 810 /* decode the complete bitstream */ | 873 /* decode the complete bitstream */ |
| 811 #ifdef SCALABLE_DEC | 874 #ifdef DRM |
| 812 if ((hDecoder->object_type == 6) || (hDecoder->object_type == DRM_ER_LC)) | 875 if (/*(hDecoder->object_type == 6) ||*/ (hDecoder->object_type == DRM_ER_LC)) |
| 813 { | 876 { |
| 814 aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc); | 877 DRM_aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc); |
| 815 } else { | 878 } else { |
| 816 #endif | 879 #endif |
| 817 raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc); | 880 raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc); |
| 818 #ifdef SCALABLE_DEC | 881 #ifdef DRM |
| 819 } | 882 } |
| 820 #endif | 883 #endif |
| 821 | 884 |
| 822 channels = hDecoder->fr_channels; | 885 channels = hDecoder->fr_channels; |
| 823 | 886 |
| 976 hInfo->sbr = SBR_DOWNSAMPLED; | 1039 hInfo->sbr = SBR_DOWNSAMPLED; |
| 977 } | 1040 } |
| 978 } | 1041 } |
| 979 #endif | 1042 #endif |
| 980 | 1043 |
| 1044 | |
| 981 sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer, | 1045 sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer, |
| 982 output_channels, frame_len, hDecoder->config.outputFormat); | 1046 output_channels, frame_len, hDecoder->config.outputFormat); |
| 1047 | |
| 1048 | |
| 1049 #ifdef DRM | |
| 1050 //conceal_output(hDecoder, frame_len, output_channels, sample_buffer); | |
| 1051 #endif | |
| 983 | 1052 |
| 984 | 1053 |
| 985 hDecoder->postSeekResetFlag = 0; | 1054 hDecoder->postSeekResetFlag = 0; |
| 986 | 1055 |
| 987 hDecoder->frame++; | 1056 hDecoder->frame++; |
| 1011 | 1080 |
| 1012 return sample_buffer; | 1081 return sample_buffer; |
| 1013 | 1082 |
| 1014 error: | 1083 error: |
| 1015 | 1084 |
| 1085 | |
| 1086 #ifdef DRM | |
| 1087 hDecoder->error_state = ERROR_STATE_INIT; | |
| 1088 #endif | |
| 1089 | |
| 1090 /* reset filterbank state */ | |
| 1091 for (i = 0; i < MAX_CHANNELS; i++) | |
| 1092 { | |
| 1093 if (hDecoder->fb_intermed[i] != NULL) | |
| 1094 { | |
| 1095 memset(hDecoder->fb_intermed[i], 0, hDecoder->frameLength*sizeof(real_t)); | |
| 1096 } | |
| 1097 } | |
| 1098 #ifdef SBR_DEC | |
| 1099 for (i = 0; i < MAX_SYNTAX_ELEMENTS; i++) | |
| 1100 { | |
| 1101 if (hDecoder->sbr[i] != NULL) | |
| 1102 { | |
| 1103 sbrReset(hDecoder->sbr[i]); | |
| 1104 } | |
| 1105 } | |
| 1106 #endif | |
| 1107 | |
| 1108 | |
| 1016 faad_endbits(&ld); | 1109 faad_endbits(&ld); |
| 1017 | 1110 |
| 1018 /* cleanup */ | 1111 /* cleanup */ |
| 1019 #ifdef ANALYSIS | 1112 #ifdef ANALYSIS |
| 1020 fflush(stdout); | 1113 fflush(stdout); |
