Mercurial > audlegacy-plugins
comparison src/aac/libfaad2/decoder.c @ 691:e6c5fdae6e88 trunk
[svn] - oh yes, commit mplayer patches as well
| author | nenolod |
|---|---|
| date | Tue, 20 Feb 2007 06:38:03 -0800 |
| parents | 1d8b08df98c3 |
| children | f1b6f1b2cdb3 |
comparison
equal
deleted
inserted
replaced
| 690:0305b3165a01 | 691:e6c5fdae6e88 |
|---|---|
| 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-2005 M. Bakker, Nero AG, http://www.nero.com | 3 ** Copyright (C) 2003-2004 M. Bakker, Ahead Software 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 | 22 ** Commercial non-GPL licensing of this software is possible. |
| 23 ** on each copy of the software: | 23 ** For more info contact Ahead Software through Mpeg4AAClicense@nero.com. |
| 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 ** | 24 ** |
| 27 ** Commercial non-GPL licensing of this software is possible. | 25 ** $Id: decoder.c,v 1.107 2004/09/08 09:43:11 gcp Exp $ |
| 28 ** For more info contact Nero AG through Mpeg4AAClicense@nero.com. | |
| 29 ** | |
| 30 ** $Id: decoder.c,v 1.109 2006/05/07 18:09:00 menno Exp $ | |
| 31 **/ | 26 **/ |
| 32 | 27 |
| 33 #include "common.h" | 28 #include "common.h" |
| 34 #include "structs.h" | 29 #include "structs.h" |
| 35 | 30 |
| 119 hDecoder->frameLength = 1024; | 114 hDecoder->frameLength = 1024; |
| 120 | 115 |
| 121 hDecoder->frame = 0; | 116 hDecoder->frame = 0; |
| 122 hDecoder->sample_buffer = NULL; | 117 hDecoder->sample_buffer = NULL; |
| 123 | 118 |
| 124 hDecoder->__r1 = 1; | |
| 125 hDecoder->__r2 = 1; | |
| 126 | |
| 127 for (i = 0; i < MAX_CHANNELS; i++) | 119 for (i = 0; i < MAX_CHANNELS; i++) |
| 128 { | 120 { |
| 129 hDecoder->window_shape_prev[i] = 0; | 121 hDecoder->window_shape_prev[i] = 0; |
| 130 hDecoder->time_out[i] = NULL; | 122 hDecoder->time_out[i] = NULL; |
| 131 hDecoder->fb_intermed[i] = NULL; | 123 hDecoder->fb_intermed[i] = NULL; |
| 200 } | 192 } |
| 201 | 193 |
| 202 return 0; | 194 return 0; |
| 203 } | 195 } |
| 204 | 196 |
| 205 | |
| 206 int32_t NEAACDECAPI NeAACDecInit(NeAACDecHandle hDecoder, uint8_t *buffer, | 197 int32_t NEAACDECAPI NeAACDecInit(NeAACDecHandle hDecoder, uint8_t *buffer, |
| 207 uint32_t buffer_size, | 198 uint32_t buffer_size, |
| 208 uint32_t *samplerate, uint8_t *channels) | 199 uint32_t *samplerate, uint8_t *channels) |
| 209 { | 200 { |
| 210 uint32_t bits = 0; | 201 uint32_t bits = 0; |
| 211 bitfile ld; | 202 bitfile ld; |
| 212 adif_header adif; | 203 adif_header adif; |
| 213 adts_header adts; | 204 adts_header adts; |
| 214 | 205 |
| 215 | |
| 216 if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL)) | 206 if ((hDecoder == NULL) || (samplerate == NULL) || (channels == NULL)) |
| 217 return -1; | 207 return -1; |
| 218 | 208 |
| 219 hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate); | 209 hDecoder->sf_index = get_sr_index(hDecoder->config.defSampleRate); |
| 220 hDecoder->object_type = hDecoder->config.defObjectType; | 210 hDecoder->object_type = hDecoder->config.defObjectType; |
| 265 faad_endbits(&ld); | 255 faad_endbits(&ld); |
| 266 return -1; | 256 return -1; |
| 267 } | 257 } |
| 268 faad_endbits(&ld); | 258 faad_endbits(&ld); |
| 269 } | 259 } |
| 260 hDecoder->channelConfiguration = *channels; | |
| 270 | 261 |
| 271 #if (defined(PS_DEC) || defined(DRM_PS)) | 262 #if (defined(PS_DEC) || defined(DRM_PS)) |
| 272 /* check if we have a mono file */ | 263 /* check if we have a mono file */ |
| 273 if (*channels == 1) | 264 if (*channels == 1) |
| 274 { | 265 { |
| 275 /* upMatrix to 2 channels for implicit signalling of PS */ | 266 /* upMatrix to 2 channels for implicit signalling of PS */ |
| 276 *channels = 2; | 267 *channels = 2; |
| 277 } | 268 } |
| 278 #endif | 269 #endif |
| 279 | |
| 280 hDecoder->channelConfiguration = *channels; | |
| 281 | 270 |
| 282 #ifdef SBR_DEC | 271 #ifdef SBR_DEC |
| 283 /* implicit signalling */ | 272 /* implicit signalling */ |
| 284 if (*samplerate <= 24000 && !(hDecoder->config.dontUpSampleImplicitSBR)) | 273 if (*samplerate <= 24000 && !(hDecoder->config.dontUpSampleImplicitSBR)) |
| 285 { | 274 { |
| 431 | 420 |
| 432 #ifdef SBR_DEC | 421 #ifdef SBR_DEC |
| 433 if ((channels == DRMCH_MONO) || (channels == DRMCH_STEREO)) | 422 if ((channels == DRMCH_MONO) || (channels == DRMCH_STEREO)) |
| 434 (*hDecoder)->sbr_present_flag = 0; | 423 (*hDecoder)->sbr_present_flag = 0; |
| 435 else | 424 else |
| 436 (*hDecoder)->sbr_present_flag = 1; | 425 (*hDecoder)->sbr_present_flag = 1; |
| 437 #endif | 426 #endif |
| 438 | 427 |
| 439 (*hDecoder)->fb = filter_bank_init((*hDecoder)->frameLength); | 428 (*hDecoder)->fb = filter_bank_init((*hDecoder)->frameLength); |
| 440 | 429 |
| 441 return 0; | 430 return 0; |
| 442 } | 431 } |
| 534 hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels; | 523 hInfo->num_lfe_channels = hDecoder->pce.num_lfe_channels; |
| 535 | 524 |
| 536 chdir = hInfo->num_front_channels; | 525 chdir = hInfo->num_front_channels; |
| 537 if (chdir & 1) | 526 if (chdir & 1) |
| 538 { | 527 { |
| 539 #if (defined(PS_DEC) || defined(DRM_PS)) | 528 hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER; |
| 540 /* When PS is enabled output is always stereo */ | 529 chdir--; |
| 530 } | |
| 531 for (i = 0; i < chdir; i += 2) | |
| 532 { | |
| 541 hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT; | 533 hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT; |
| 542 hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT; | 534 hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT; |
| 543 #else | |
| 544 hInfo->channel_position[chpos++] = FRONT_CHANNEL_CENTER; | |
| 545 chdir--; | |
| 546 #endif | |
| 547 } | |
| 548 for (i = 0; i < chdir; i += 2) | |
| 549 { | |
| 550 hInfo->channel_position[chpos++] = FRONT_CHANNEL_LEFT; | |
| 551 hInfo->channel_position[chpos++] = FRONT_CHANNEL_RIGHT; | |
| 552 } | 535 } |
| 553 | 536 |
| 554 for (i = 0; i < hInfo->num_side_channels; i += 2) | 537 for (i = 0; i < hInfo->num_side_channels; i += 2) |
| 555 { | 538 { |
| 556 hInfo->channel_position[chpos++] = SIDE_CHANNEL_LEFT; | 539 hInfo->channel_position[chpos++] = SIDE_CHANNEL_LEFT; |
| 580 | 563 |
| 581 } else { | 564 } else { |
| 582 switch (hDecoder->channelConfiguration) | 565 switch (hDecoder->channelConfiguration) |
| 583 { | 566 { |
| 584 case 1: | 567 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 | |
| 591 hInfo->num_front_channels = 1; | 568 hInfo->num_front_channels = 1; |
| 592 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; | 569 hInfo->channel_position[0] = FRONT_CHANNEL_CENTER; |
| 593 #endif | |
| 594 break; | 570 break; |
| 595 case 2: | 571 case 2: |
| 596 hInfo->num_front_channels = 2; | 572 hInfo->num_front_channels = 2; |
| 597 hInfo->channel_position[0] = FRONT_CHANNEL_LEFT; | 573 hInfo->channel_position[0] = FRONT_CHANNEL_LEFT; |
| 598 hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT; | 574 hInfo->channel_position[1] = FRONT_CHANNEL_RIGHT; |
| 744 | 720 |
| 745 return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size, | 721 return aac_frame_decode(hDecoder, hInfo, buffer, buffer_size, |
| 746 sample_buffer, sample_buffer_size); | 722 sample_buffer, sample_buffer_size); |
| 747 } | 723 } |
| 748 | 724 |
| 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 | |
| 760 static void* aac_frame_decode(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo, | 725 static void* aac_frame_decode(NeAACDecHandle hDecoder, NeAACDecFrameInfo *hInfo, |
| 761 uint8_t *buffer, uint32_t buffer_size, | 726 uint8_t *buffer, uint32_t buffer_size, |
| 762 void **sample_buffer2, uint32_t sample_buffer_size) | 727 void **sample_buffer2, uint32_t sample_buffer_size) |
| 763 { | 728 { |
| 764 uint16_t i; | |
| 765 uint8_t channels = 0; | 729 uint8_t channels = 0; |
| 766 uint8_t output_channels = 0; | 730 uint8_t output_channels = 0; |
| 767 bitfile ld = {0}; | 731 bitfile ld; |
| 768 uint32_t bitsconsumed; | 732 uint32_t bitsconsumed; |
| 769 uint16_t frame_len; | 733 uint16_t frame_len; |
| 770 void *sample_buffer; | 734 void *sample_buffer; |
| 771 | 735 |
| 772 #ifdef PROFILE | 736 #ifdef PROFILE |
| 786 frame_len = hDecoder->frameLength; | 750 frame_len = hDecoder->frameLength; |
| 787 | 751 |
| 788 | 752 |
| 789 memset(hInfo, 0, sizeof(NeAACDecFrameInfo)); | 753 memset(hInfo, 0, sizeof(NeAACDecFrameInfo)); |
| 790 memset(hDecoder->internal_channel, 0, MAX_CHANNELS*sizeof(hDecoder->internal_channel[0])); | 754 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 | |
| 818 | 755 |
| 819 /* initialize the bitstream */ | 756 /* initialize the bitstream */ |
| 820 faad_initbits(&ld, buffer, buffer_size); | 757 faad_initbits(&ld, buffer, buffer_size); |
| 821 | 758 |
| 822 #if 0 | 759 #if 0 |
| 841 if (hDecoder->object_type == DRM_ER_LC) | 778 if (hDecoder->object_type == DRM_ER_LC) |
| 842 { | 779 { |
| 843 /* We do not support stereo right now */ | 780 /* We do not support stereo right now */ |
| 844 if (0) //(hDecoder->channelConfiguration == 2) | 781 if (0) //(hDecoder->channelConfiguration == 2) |
| 845 { | 782 { |
| 846 hInfo->error = 28; // Throw CRC error | 783 hInfo->error = 8; // Throw CRC error |
| 847 goto error; | 784 goto error; |
| 848 } | 785 } |
| 849 | 786 |
| 850 faad_getbits(&ld, 8 | 787 faad_getbits(&ld, 8 |
| 851 DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC")); | 788 DEBUGVAR(1,1,"NeAACDecDecode(): skip CRC")); |
| 869 #ifdef ANALYSIS | 806 #ifdef ANALYSIS |
| 870 dbg_count = 0; | 807 dbg_count = 0; |
| 871 #endif | 808 #endif |
| 872 | 809 |
| 873 /* decode the complete bitstream */ | 810 /* decode the complete bitstream */ |
| 874 #ifdef DRM | 811 #ifdef SCALABLE_DEC |
| 875 if (/*(hDecoder->object_type == 6) ||*/ (hDecoder->object_type == DRM_ER_LC)) | 812 if ((hDecoder->object_type == 6) || (hDecoder->object_type == DRM_ER_LC)) |
| 876 { | 813 { |
| 877 DRM_aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc); | 814 aac_scalable_main_element(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc); |
| 878 } else { | 815 } else { |
| 879 #endif | 816 #endif |
| 880 raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc); | 817 raw_data_block(hDecoder, hInfo, &ld, &hDecoder->pce, hDecoder->drc); |
| 881 #ifdef DRM | 818 #ifdef SCALABLE_DEC |
| 882 } | 819 } |
| 883 #endif | 820 #endif |
| 884 | 821 |
| 885 channels = hDecoder->fr_channels; | 822 channels = hDecoder->fr_channels; |
| 886 | 823 |
| 1039 hInfo->sbr = SBR_DOWNSAMPLED; | 976 hInfo->sbr = SBR_DOWNSAMPLED; |
| 1040 } | 977 } |
| 1041 } | 978 } |
| 1042 #endif | 979 #endif |
| 1043 | 980 |
| 1044 | |
| 1045 sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer, | 981 sample_buffer = output_to_PCM(hDecoder, hDecoder->time_out, sample_buffer, |
| 1046 output_channels, frame_len, hDecoder->config.outputFormat); | 982 output_channels, frame_len, hDecoder->config.outputFormat); |
| 1047 | |
| 1048 | |
| 1049 #ifdef DRM | |
| 1050 //conceal_output(hDecoder, frame_len, output_channels, sample_buffer); | |
| 1051 #endif | |
| 1052 | 983 |
| 1053 | 984 |
| 1054 hDecoder->postSeekResetFlag = 0; | 985 hDecoder->postSeekResetFlag = 0; |
| 1055 | 986 |
| 1056 hDecoder->frame++; | 987 hDecoder->frame++; |
| 1080 | 1011 |
| 1081 return sample_buffer; | 1012 return sample_buffer; |
| 1082 | 1013 |
| 1083 error: | 1014 error: |
| 1084 | 1015 |
| 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 | |
| 1109 faad_endbits(&ld); | 1016 faad_endbits(&ld); |
| 1110 | 1017 |
| 1111 /* cleanup */ | 1018 /* cleanup */ |
| 1112 #ifdef ANALYSIS | 1019 #ifdef ANALYSIS |
| 1113 fflush(stdout); | 1020 fflush(stdout); |
