Mercurial > libavcodec.hg
comparison mpegaudiodec.c @ 2507:0334caf0f0ce libavcodec
Multichannel mp3 in mp4 support ISO/IEC 14496-3:2001/FPDAM 3 (MP3onMP4)
Derived from MPlayer patch by Larry Ruedisueli
author | rtognimp |
---|---|
date | Tue, 15 Feb 2005 20:58:32 +0000 |
parents | 74d7fd7b49c5 |
children | e25782262d7d |
comparison
equal
deleted
inserted
replaced
2506:9404bbf9de07 | 2507:0334caf0f0ce |
---|---|
117 #endif | 117 #endif |
118 void (*compute_antialias)(struct MPADecodeContext *s, struct GranuleDef *g); | 118 void (*compute_antialias)(struct MPADecodeContext *s, struct GranuleDef *g); |
119 int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3 | 119 int adu_mode; ///< 0 for standard mp3, 1 for adu formatted mp3 |
120 unsigned int dither_state; | 120 unsigned int dither_state; |
121 } MPADecodeContext; | 121 } MPADecodeContext; |
122 | |
123 /** | |
124 * Context for MP3On4 decoder | |
125 */ | |
126 typedef struct MP3On4DecodeContext { | |
127 int frames; ///< number of mp3 frames per block (number of mp3 decoder instances) | |
128 int chan_cfg; ///< channel config number | |
129 MPADecodeContext *mp3decctx[5]; ///< MPADecodeContext for every decoder instance | |
130 } MP3On4DecodeContext; | |
122 | 131 |
123 /* layer 3 "granule" */ | 132 /* layer 3 "granule" */ |
124 typedef struct GranuleDef { | 133 typedef struct GranuleDef { |
125 uint8_t scfsi; | 134 uint8_t scfsi; |
126 int part2_3_length; | 135 int part2_3_length; |
2676 *data_size = out_size; | 2685 *data_size = out_size; |
2677 return buf_size; | 2686 return buf_size; |
2678 } | 2687 } |
2679 | 2688 |
2680 | 2689 |
2690 /* Next 3 arrays are indexed by channel config number (passed via codecdata) */ | |
2691 static int mp3Frames[16] = {0,1,1,2,3,3,4,5,2}; /* number of mp3 decoder instances */ | |
2692 static int mp3Channels[16] = {0,1,2,3,4,5,6,8,4}; /* total output channels */ | |
2693 /* offsets into output buffer, assume output order is FL FR BL BR C LFE */ | |
2694 static int chan_offset[9][5] = { | |
2695 {0}, | |
2696 {0}, // C | |
2697 {0}, // FLR | |
2698 {2,0}, // C FLR | |
2699 {2,0,3}, // C FLR BS | |
2700 {4,0,2}, // C FLR BLRS | |
2701 {4,0,2,5}, // C FLR BLRS LFE | |
2702 {4,0,2,6,5}, // C FLR BLRS BLR LFE | |
2703 {0,2} // FLR BLRS | |
2704 }; | |
2705 | |
2706 | |
2707 static int decode_init_mp3on4(AVCodecContext * avctx) | |
2708 { | |
2709 MP3On4DecodeContext *s = avctx->priv_data; | |
2710 int i; | |
2711 | |
2712 if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) { | |
2713 av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n"); | |
2714 return -1; | |
2715 } | |
2716 | |
2717 s->chan_cfg = (((unsigned char *)avctx->extradata)[1] >> 3) & 0x0f; | |
2718 s->frames = mp3Frames[s->chan_cfg]; | |
2719 if(!s->frames) { | |
2720 av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n"); | |
2721 return -1; | |
2722 } | |
2723 avctx->channels = mp3Channels[s->chan_cfg]; | |
2724 | |
2725 /* Init the first mp3 decoder in standard way, so that all tables get builded | |
2726 * We replace avctx->priv_data with the context of the first decoder so that | |
2727 * decode_init() does not have to be changed. | |
2728 * Other decoders will be inited here copying data from the first context | |
2729 */ | |
2730 // Allocate zeroed memory for the first decoder context | |
2731 s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext)); | |
2732 // Put decoder context in place to make init_decode() happy | |
2733 avctx->priv_data = s->mp3decctx[0]; | |
2734 decode_init(avctx); | |
2735 // Restore mp3on4 context pointer | |
2736 avctx->priv_data = s; | |
2737 s->mp3decctx[0]->adu_mode = 1; // Set adu mode | |
2738 | |
2739 /* Create a separate codec/context for each frame (first is already ok). | |
2740 * Each frame is 1 or 2 channels - up to 5 frames allowed | |
2741 */ | |
2742 for (i = 1; i < s->frames; i++) { | |
2743 s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext)); | |
2744 s->mp3decctx[i]->compute_antialias = s->mp3decctx[0]->compute_antialias; | |
2745 s->mp3decctx[i]->inbuf = &s->mp3decctx[i]->inbuf1[0][BACKSTEP_SIZE]; | |
2746 s->mp3decctx[i]->inbuf_ptr = s->mp3decctx[i]->inbuf; | |
2747 s->mp3decctx[i]->adu_mode = 1; | |
2748 } | |
2749 | |
2750 return 0; | |
2751 } | |
2752 | |
2753 | |
2754 static int decode_close_mp3on4(AVCodecContext * avctx) | |
2755 { | |
2756 MP3On4DecodeContext *s = avctx->priv_data; | |
2757 int i; | |
2758 | |
2759 for (i = 0; i < s->frames; i++) | |
2760 if (s->mp3decctx[i]) | |
2761 av_free(s->mp3decctx[i]); | |
2762 | |
2763 return 0; | |
2764 } | |
2765 | |
2766 | |
2767 static int decode_frame_mp3on4(AVCodecContext * avctx, | |
2768 void *data, int *data_size, | |
2769 uint8_t * buf, int buf_size) | |
2770 { | |
2771 MP3On4DecodeContext *s = avctx->priv_data; | |
2772 MPADecodeContext *m; | |
2773 int len, out_size = 0; | |
2774 uint32_t header; | |
2775 OUT_INT *out_samples = data; | |
2776 OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS]; | |
2777 OUT_INT *outptr, *bp; | |
2778 int fsize; | |
2779 unsigned char *start2 = buf, *start; | |
2780 int fr, i, j, n; | |
2781 int off = avctx->channels; | |
2782 int *coff = chan_offset[s->chan_cfg]; | |
2783 | |
2784 len = buf_size; | |
2785 | |
2786 // Discard too short frames | |
2787 if (buf_size < HEADER_SIZE) { | |
2788 *data_size = 0; | |
2789 return buf_size; | |
2790 } | |
2791 | |
2792 // If only one decoder interleave is not needed | |
2793 outptr = s->frames == 1 ? out_samples : decoded_buf; | |
2794 | |
2795 for (fr = 0; fr < s->frames; fr++) { | |
2796 start = start2; | |
2797 fsize = (start[0] << 4) | (start[1] >> 4); | |
2798 start2 += fsize; | |
2799 if (fsize > len) | |
2800 fsize = len; | |
2801 len -= fsize; | |
2802 if (fsize > MPA_MAX_CODED_FRAME_SIZE) | |
2803 fsize = MPA_MAX_CODED_FRAME_SIZE; | |
2804 m = s->mp3decctx[fr]; | |
2805 assert (m != NULL); | |
2806 /* copy original to new */ | |
2807 m->inbuf_ptr = m->inbuf + fsize; | |
2808 memcpy(m->inbuf, start, fsize); | |
2809 | |
2810 // Get header | |
2811 header = (m->inbuf[0] << 24) | (m->inbuf[1] << 16) | | |
2812 (m->inbuf[2] << 8) | m->inbuf[3] | 0xfff00000; | |
2813 | |
2814 if (ff_mpa_check_header(header) < 0) { // Bad header, discard block | |
2815 *data_size = 0; | |
2816 return buf_size; | |
2817 } | |
2818 | |
2819 decode_header(m, header); | |
2820 mp_decode_frame(m, decoded_buf); | |
2821 | |
2822 n = MPA_FRAME_SIZE * m->nb_channels; | |
2823 out_size += n * sizeof(OUT_INT); | |
2824 if(s->frames > 1) { | |
2825 /* interleave output data */ | |
2826 bp = out_samples + coff[fr]; | |
2827 if(m->nb_channels == 1) { | |
2828 for(j = 0; j < n; j++) { | |
2829 *bp = decoded_buf[j]; | |
2830 bp += off; | |
2831 } | |
2832 } else { | |
2833 for(j = 0; j < n; j++) { | |
2834 bp[0] = decoded_buf[j++]; | |
2835 bp[1] = decoded_buf[j]; | |
2836 bp += off; | |
2837 } | |
2838 } | |
2839 } | |
2840 } | |
2841 | |
2842 /* update codec info */ | |
2843 avctx->sample_rate = s->mp3decctx[0]->sample_rate; | |
2844 avctx->frame_size= buf_size; | |
2845 avctx->bit_rate = 0; | |
2846 for (i = 0; i < s->frames; i++) | |
2847 avctx->bit_rate += s->mp3decctx[i]->bit_rate; | |
2848 | |
2849 *data_size = out_size; | |
2850 return buf_size; | |
2851 } | |
2852 | |
2853 | |
2681 AVCodec mp2_decoder = | 2854 AVCodec mp2_decoder = |
2682 { | 2855 { |
2683 "mp2", | 2856 "mp2", |
2684 CODEC_TYPE_AUDIO, | 2857 CODEC_TYPE_AUDIO, |
2685 CODEC_ID_MP2, | 2858 CODEC_ID_MP2, |
2714 NULL, | 2887 NULL, |
2715 NULL, | 2888 NULL, |
2716 decode_frame_adu, | 2889 decode_frame_adu, |
2717 CODEC_CAP_PARSE_ONLY, | 2890 CODEC_CAP_PARSE_ONLY, |
2718 }; | 2891 }; |
2892 | |
2893 AVCodec mp3on4_decoder = | |
2894 { | |
2895 "mp3on4", | |
2896 CODEC_TYPE_AUDIO, | |
2897 CODEC_ID_MP3ON4, | |
2898 sizeof(MP3On4DecodeContext), | |
2899 decode_init_mp3on4, | |
2900 NULL, | |
2901 decode_close_mp3on4, | |
2902 decode_frame_mp3on4, | |
2903 0 | |
2904 }; |