Mercurial > libavcodec.hg
comparison mpegaudiodec.c @ 1428:aece5836c588 libavcodec
fixed audio frame buffering problem (should correct problems on some streams) - faster synthesis filter - prototype 'parse_only' support
author | bellard |
---|---|
date | Thu, 28 Aug 2003 13:38:06 +0000 |
parents | 5ee7bd7ee76d |
children | 317ba7ab73bd |
comparison
equal
deleted
inserted
replaced
1427:3a6d613c88d8 | 1428:aece5836c588 |
---|---|
308 { | 308 { |
309 MPADecodeContext *s = avctx->priv_data; | 309 MPADecodeContext *s = avctx->priv_data; |
310 static int init=0; | 310 static int init=0; |
311 int i, j, k; | 311 int i, j, k; |
312 | 312 |
313 if(!init) { | 313 if (!init && !avctx->parse_only) { |
314 /* scale factors table for layer 1/2 */ | 314 /* scale factors table for layer 1/2 */ |
315 for(i=0;i<64;i++) { | 315 for(i=0;i<64;i++) { |
316 int shift, mod; | 316 int shift, mod; |
317 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */ | 317 /* 1.0 (i = 3) is normalized to 2 ^ FRAC_BITS */ |
318 shift = (i / 3); | 318 shift = (i / 3); |
735 | 735 |
736 #define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 15) | 736 #define OUT_SHIFT (WFRAC_BITS + FRAC_BITS - 15) |
737 | 737 |
738 #if FRAC_BITS <= 15 | 738 #if FRAC_BITS <= 15 |
739 | 739 |
740 #define OUT_SAMPLE(sum)\ | 740 static inline int round_sample(int sum) |
741 {\ | 741 { |
742 int sum1;\ | 742 int sum1; |
743 sum1 = (sum + (1 << (OUT_SHIFT - 1))) >> OUT_SHIFT;\ | 743 sum1 = (sum + (1 << (OUT_SHIFT - 1))) >> OUT_SHIFT; |
744 if (sum1 < -32768)\ | 744 if (sum1 < -32768) |
745 sum1 = -32768;\ | 745 sum1 = -32768; |
746 else if (sum1 > 32767)\ | 746 else if (sum1 > 32767) |
747 sum1 = 32767;\ | 747 sum1 = 32767; |
748 *samples = sum1;\ | 748 return sum1; |
749 samples += incr;\ | 749 } |
750 } | 750 |
751 | 751 #if defined(ARCH_POWERPC_405) |
752 #define SUM8(off, op) \ | 752 |
753 /* signed 16x16 -> 32 multiply add accumulate */ | |
754 #define MACS(rt, ra, rb) \ | |
755 asm ("maclhw %0, %2, %3" : "=r" (rt) : "0" (rt), "r" (ra), "r" (rb)); | |
756 | |
757 /* signed 16x16 -> 32 multiply */ | |
758 #define MULS(ra, rb) \ | |
759 ({ int __rt; asm ("mullhw %0, %1, %2" : "=r" (__rt) : "r" (ra), "r" (rb)); __rt; }) | |
760 | |
761 #else | |
762 | |
763 /* signed 16x16 -> 32 multiply add accumulate */ | |
764 #define MACS(rt, ra, rb) rt += (ra) * (rb) | |
765 | |
766 /* signed 16x16 -> 32 multiply */ | |
767 #define MULS(ra, rb) ((ra) * (rb)) | |
768 | |
769 #endif | |
770 | |
771 #else | |
772 | |
773 static inline int round_sample(int64_t sum) | |
774 { | |
775 int sum1; | |
776 sum1 = (int)((sum + (int64_t_C(1) << (OUT_SHIFT - 1))) >> OUT_SHIFT); | |
777 if (sum1 < -32768) | |
778 sum1 = -32768; | |
779 else if (sum1 > 32767) | |
780 sum1 = 32767; | |
781 return sum1; | |
782 } | |
783 | |
784 #define MULS(ra, rb) MUL64(ra, rb) | |
785 | |
786 #endif | |
787 | |
788 #define SUM8(sum, op, w, p) \ | |
753 { \ | 789 { \ |
754 sum op w[0 * 64 + off] * p[0 * 64];\ | 790 sum op MULS((w)[0 * 64], p[0 * 64]);\ |
755 sum op w[1 * 64 + off] * p[1 * 64];\ | 791 sum op MULS((w)[1 * 64], p[1 * 64]);\ |
756 sum op w[2 * 64 + off] * p[2 * 64];\ | 792 sum op MULS((w)[2 * 64], p[2 * 64]);\ |
757 sum op w[3 * 64 + off] * p[3 * 64];\ | 793 sum op MULS((w)[3 * 64], p[3 * 64]);\ |
758 sum op w[4 * 64 + off] * p[4 * 64];\ | 794 sum op MULS((w)[4 * 64], p[4 * 64]);\ |
759 sum op w[5 * 64 + off] * p[5 * 64];\ | 795 sum op MULS((w)[5 * 64], p[5 * 64]);\ |
760 sum op w[6 * 64 + off] * p[6 * 64];\ | 796 sum op MULS((w)[6 * 64], p[6 * 64]);\ |
761 sum op w[7 * 64 + off] * p[7 * 64];\ | 797 sum op MULS((w)[7 * 64], p[7 * 64]);\ |
762 } | 798 } |
763 | 799 |
764 #else | 800 #define SUM8P2(sum1, op1, sum2, op2, w1, w2, p) \ |
765 | |
766 #define OUT_SAMPLE(sum)\ | |
767 {\ | |
768 int sum1;\ | |
769 sum1 = (int)((sum + (int64_t_C(1) << (OUT_SHIFT - 1))) >> OUT_SHIFT);\ | |
770 if (sum1 < -32768)\ | |
771 sum1 = -32768;\ | |
772 else if (sum1 > 32767)\ | |
773 sum1 = 32767;\ | |
774 *samples = sum1;\ | |
775 samples += incr;\ | |
776 } | |
777 | |
778 #define SUM8(off, op) \ | |
779 { \ | 801 { \ |
780 sum op MUL64(w[0 * 64 + off], p[0 * 64]);\ | 802 int tmp;\ |
781 sum op MUL64(w[1 * 64 + off], p[1 * 64]);\ | 803 tmp = p[0 * 64];\ |
782 sum op MUL64(w[2 * 64 + off], p[2 * 64]);\ | 804 sum1 op1 MULS((w1)[0 * 64], tmp);\ |
783 sum op MUL64(w[3 * 64 + off], p[3 * 64]);\ | 805 sum2 op2 MULS((w2)[0 * 64], tmp);\ |
784 sum op MUL64(w[4 * 64 + off], p[4 * 64]);\ | 806 tmp = p[1 * 64];\ |
785 sum op MUL64(w[5 * 64 + off], p[5 * 64]);\ | 807 sum1 op1 MULS((w1)[1 * 64], tmp);\ |
786 sum op MUL64(w[6 * 64 + off], p[6 * 64]);\ | 808 sum2 op2 MULS((w2)[1 * 64], tmp);\ |
787 sum op MUL64(w[7 * 64 + off], p[7 * 64]);\ | 809 tmp = p[2 * 64];\ |
788 } | 810 sum1 op1 MULS((w1)[2 * 64], tmp);\ |
789 | 811 sum2 op2 MULS((w2)[2 * 64], tmp);\ |
790 #endif | 812 tmp = p[3 * 64];\ |
813 sum1 op1 MULS((w1)[3 * 64], tmp);\ | |
814 sum2 op2 MULS((w2)[3 * 64], tmp);\ | |
815 tmp = p[4 * 64];\ | |
816 sum1 op1 MULS((w1)[4 * 64], tmp);\ | |
817 sum2 op2 MULS((w2)[4 * 64], tmp);\ | |
818 tmp = p[5 * 64];\ | |
819 sum1 op1 MULS((w1)[5 * 64], tmp);\ | |
820 sum2 op2 MULS((w2)[5 * 64], tmp);\ | |
821 tmp = p[6 * 64];\ | |
822 sum1 op1 MULS((w1)[6 * 64], tmp);\ | |
823 sum2 op2 MULS((w2)[6 * 64], tmp);\ | |
824 tmp = p[7 * 64];\ | |
825 sum1 op1 MULS((w1)[7 * 64], tmp);\ | |
826 sum2 op2 MULS((w2)[7 * 64], tmp);\ | |
827 } | |
828 | |
791 | 829 |
792 /* 32 sub band synthesis filter. Input: 32 sub band samples, Output: | 830 /* 32 sub band synthesis filter. Input: 32 sub band samples, Output: |
793 32 samples. */ | 831 32 samples. */ |
794 /* XXX: optimize by avoiding ring buffer usage */ | 832 /* XXX: optimize by avoiding ring buffer usage */ |
795 static void synth_filter(MPADecodeContext *s1, | 833 static void synth_filter(MPADecodeContext *s1, |
796 int ch, int16_t *samples, int incr, | 834 int ch, int16_t *samples, int incr, |
797 int32_t sb_samples[SBLIMIT]) | 835 int32_t sb_samples[SBLIMIT]) |
798 { | 836 { |
799 int32_t tmp[32]; | 837 int32_t tmp[32]; |
800 register MPA_INT *synth_buf, *p; | 838 register MPA_INT *synth_buf; |
801 register MPA_INT *w; | 839 const register MPA_INT *w, *w2, *p; |
802 int j, offset, v; | 840 int j, offset, v; |
841 int16_t *samples2; | |
803 #if FRAC_BITS <= 15 | 842 #if FRAC_BITS <= 15 |
804 int sum; | 843 int sum, sum2; |
805 #else | 844 #else |
806 int64_t sum; | 845 int64_t sum, sum2; |
807 #endif | 846 #endif |
808 | 847 |
809 dct32(tmp, sb_samples); | 848 dct32(tmp, sb_samples); |
810 | 849 |
811 offset = s1->synth_buf_offset[ch]; | 850 offset = s1->synth_buf_offset[ch]; |
812 synth_buf = s1->synth_buf[ch] + offset; | 851 synth_buf = s1->synth_buf[ch] + offset; |
813 | 852 |
824 synth_buf[j] = v; | 863 synth_buf[j] = v; |
825 } | 864 } |
826 /* copy to avoid wrap */ | 865 /* copy to avoid wrap */ |
827 memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT)); | 866 memcpy(synth_buf + 512, synth_buf, 32 * sizeof(MPA_INT)); |
828 | 867 |
868 samples2 = samples + 31 * incr; | |
829 w = window; | 869 w = window; |
830 for(j=0;j<16;j++) { | 870 w2 = window + 31; |
871 | |
872 sum = 0; | |
873 p = synth_buf + 16; | |
874 SUM8(sum, +=, w, p); | |
875 p = synth_buf + 48; | |
876 SUM8(sum, -=, w + 32, p); | |
877 *samples = round_sample(sum); | |
878 samples += incr; | |
879 w++; | |
880 | |
881 /* we calculate two samples at the same time to avoid one memory | |
882 access per two sample */ | |
883 for(j=1;j<16;j++) { | |
831 sum = 0; | 884 sum = 0; |
832 p = synth_buf + 16 + j; /* 0-15 */ | 885 sum2 = 0; |
833 SUM8(0, +=); | 886 p = synth_buf + 16 + j; |
834 p = synth_buf + 48 - j; /* 32-47 */ | 887 SUM8P2(sum, +=, sum2, -=, w, w2, p); |
835 SUM8(32, -=); | 888 p = synth_buf + 48 - j; |
836 OUT_SAMPLE(sum); | 889 SUM8P2(sum, -=, sum2, -=, w + 32, w2 + 32, p); |
890 | |
891 *samples = round_sample(sum); | |
892 samples += incr; | |
893 *samples2 = round_sample(sum2); | |
894 samples2 -= incr; | |
837 w++; | 895 w++; |
838 } | 896 w2--; |
839 | 897 } |
840 p = synth_buf + 32; /* 48 */ | 898 |
899 p = synth_buf + 32; | |
841 sum = 0; | 900 sum = 0; |
842 SUM8(32, -=); | 901 SUM8(sum, -=, w + 32, p); |
843 OUT_SAMPLE(sum); | 902 *samples = round_sample(sum); |
844 w++; | 903 |
845 | |
846 for(j=17;j<32;j++) { | |
847 sum = 0; | |
848 p = synth_buf + 48 - j; /* 17-31 */ | |
849 SUM8(0, -=); | |
850 p = synth_buf + 16 + j; /* 49-63 */ | |
851 SUM8(32, -=); | |
852 OUT_SAMPLE(sum); | |
853 w++; | |
854 } | |
855 offset = (offset - 32) & 511; | 904 offset = (offset - 32) & 511; |
856 s1->synth_buf_offset[ch] = offset; | 905 s1->synth_buf_offset[ch] = offset; |
857 } | 906 } |
858 | 907 |
859 /* cos(pi*i/24) */ | 908 /* cos(pi*i/24) */ |
1152 } else { | 1201 } else { |
1153 printf("mono"); | 1202 printf("mono"); |
1154 } | 1203 } |
1155 printf("\n"); | 1204 printf("\n"); |
1156 #endif | 1205 #endif |
1206 return 0; | |
1207 } | |
1208 | |
1209 /* useful helper to get mpeg audio stream infos. Return -1 if error in | |
1210 header */ | |
1211 int mp_decode_header(int *sample_rate_ptr, | |
1212 int *nb_channels_ptr, | |
1213 int *coded_frame_size_ptr, | |
1214 int *decoded_frame_size_ptr, | |
1215 uint32_t head) | |
1216 { | |
1217 MPADecodeContext s1, *s = &s1; | |
1218 int decoded_frame_size; | |
1219 | |
1220 if (check_header(head) != 0) | |
1221 return -1; | |
1222 | |
1223 if (decode_header(s, head) != 0) { | |
1224 return -1; | |
1225 } | |
1226 | |
1227 switch(s->layer) { | |
1228 case 1: | |
1229 decoded_frame_size = 384; | |
1230 break; | |
1231 case 2: | |
1232 decoded_frame_size = 1152; | |
1233 break; | |
1234 default: | |
1235 case 3: | |
1236 if (s->lsf) | |
1237 decoded_frame_size = 576; | |
1238 else | |
1239 decoded_frame_size = 1152; | |
1240 break; | |
1241 } | |
1242 | |
1243 *sample_rate_ptr = s->sample_rate; | |
1244 *nb_channels_ptr = s->nb_channels; | |
1245 *coded_frame_size_ptr = s->frame_size; | |
1246 *decoded_frame_size_ptr = decoded_frame_size * 2 * s->nb_channels; | |
1157 return 0; | 1247 return 0; |
1158 } | 1248 } |
1159 | 1249 |
1160 /* return the number of decoded frames */ | 1250 /* return the number of decoded frames */ |
1161 static int mp_decode_layer1(MPADecodeContext *s) | 1251 static int mp_decode_layer1(MPADecodeContext *s) |
2389 } | 2479 } |
2390 /* update codec info */ | 2480 /* update codec info */ |
2391 avctx->sample_rate = s->sample_rate; | 2481 avctx->sample_rate = s->sample_rate; |
2392 avctx->channels = s->nb_channels; | 2482 avctx->channels = s->nb_channels; |
2393 avctx->bit_rate = s->bit_rate; | 2483 avctx->bit_rate = s->bit_rate; |
2394 avctx->frame_size = s->frame_size; | 2484 switch(s->layer) { |
2485 case 1: | |
2486 avctx->frame_size = 384; | |
2487 break; | |
2488 case 2: | |
2489 avctx->frame_size = 1152; | |
2490 break; | |
2491 case 3: | |
2492 if (s->lsf) | |
2493 avctx->frame_size = 576; | |
2494 else | |
2495 avctx->frame_size = 1152; | |
2496 break; | |
2497 } | |
2395 } | 2498 } |
2396 } | 2499 } |
2397 } else if (s->frame_size == -1) { | 2500 } else if (s->frame_size == -1) { |
2398 /* free format : find next sync to compute frame size */ | 2501 /* free format : find next sync to compute frame size */ |
2399 len = MPA_MAX_CODED_FRAME_SIZE - len; | 2502 len = MPA_MAX_CODED_FRAME_SIZE - len; |
2455 len = buf_size; | 2558 len = buf_size; |
2456 memcpy(s->inbuf_ptr, buf_ptr, len); | 2559 memcpy(s->inbuf_ptr, buf_ptr, len); |
2457 buf_ptr += len; | 2560 buf_ptr += len; |
2458 s->inbuf_ptr += len; | 2561 s->inbuf_ptr += len; |
2459 buf_size -= len; | 2562 buf_size -= len; |
2460 } else { | 2563 } |
2461 out_size = mp_decode_frame(s, out_samples); | 2564 next_data: |
2565 if (s->frame_size > 0 && | |
2566 (s->inbuf_ptr - s->inbuf) >= s->frame_size) { | |
2567 if (avctx->parse_only) { | |
2568 /* simply return the frame data */ | |
2569 *(uint8_t **)data = s->inbuf; | |
2570 out_size = s->inbuf_ptr - s->inbuf; | |
2571 } else { | |
2572 out_size = mp_decode_frame(s, out_samples); | |
2573 } | |
2462 s->inbuf_ptr = s->inbuf; | 2574 s->inbuf_ptr = s->inbuf; |
2463 s->frame_size = 0; | 2575 s->frame_size = 0; |
2464 *data_size = out_size; | 2576 *data_size = out_size; |
2465 break; | 2577 break; |
2466 } | 2578 } |
2467 next_data: | |
2468 ; | |
2469 } | 2579 } |
2470 return buf_ptr - buf; | 2580 return buf_ptr - buf; |
2471 } | 2581 } |
2472 | 2582 |
2473 AVCodec mp2_decoder = | 2583 AVCodec mp2_decoder = |
2478 sizeof(MPADecodeContext), | 2588 sizeof(MPADecodeContext), |
2479 decode_init, | 2589 decode_init, |
2480 NULL, | 2590 NULL, |
2481 NULL, | 2591 NULL, |
2482 decode_frame, | 2592 decode_frame, |
2593 CODEC_CAP_PARSE_ONLY, | |
2483 }; | 2594 }; |
2484 | 2595 |
2485 AVCodec mp3_decoder = | 2596 AVCodec mp3_decoder = |
2486 { | 2597 { |
2487 "mp3", | 2598 "mp3", |
2490 sizeof(MPADecodeContext), | 2601 sizeof(MPADecodeContext), |
2491 decode_init, | 2602 decode_init, |
2492 NULL, | 2603 NULL, |
2493 NULL, | 2604 NULL, |
2494 decode_frame, | 2605 decode_frame, |
2606 CODEC_CAP_PARSE_ONLY, | |
2495 }; | 2607 }; |
2496 | |
2497 #undef C1 | |
2498 #undef C2 | |
2499 #undef C3 | |
2500 #undef C4 | |
2501 #undef C5 | |
2502 #undef C6 | |
2503 #undef C7 | |
2504 #undef C8 | |
2505 #undef FRAC_BITS | |
2506 #undef HEADER_SIZE |