- short *out_samples = data;
-
- *data_size = 0;
- buf_ptr = buf;
- while (buf_size > 0) {
- len = s->inbuf_ptr - s->inbuf;
- if (s->frame_size == 0) {
- /* special case for next header for first frame in free
- format case (XXX: find a simpler method) */
- if (s->free_format_next_header != 0) {
- s->inbuf[0] = s->free_format_next_header >> 24;
- s->inbuf[1] = s->free_format_next_header >> 16;
- s->inbuf[2] = s->free_format_next_header >> 8;
- s->inbuf[3] = s->free_format_next_header;
- s->inbuf_ptr = s->inbuf + 4;
- s->free_format_next_header = 0;
- goto got_header;
- }
- /* no header seen : find one. We need at least HEADER_SIZE
- bytes to parse it */
- len = HEADER_SIZE - len;
- if (len > buf_size)
- len = buf_size;
- if (len > 0) {
- memcpy(s->inbuf_ptr, buf_ptr, len);
- buf_ptr += len;
- buf_size -= len;
- s->inbuf_ptr += len;
- }
- if ((s->inbuf_ptr - s->inbuf) >= HEADER_SIZE) {
- got_header:
- header = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
- (s->inbuf[2] << 8) | s->inbuf[3];
-
- if (check_header(header) < 0) {
- /* no sync found : move by one byte (inefficient, but simple!) */
- memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
- s->inbuf_ptr--;
- dprintf("skip %x\n", header);
- /* reset free format frame size to give a chance
- to get a new bitrate */
- s->free_format_frame_size = 0;
- } else {
- if (decode_header(s, header) == 1) {
- /* free format: prepare to compute frame size */
- s->frame_size = -1;
- }
- /* update codec info */
- avctx->sample_rate = s->sample_rate;
- avctx->channels = s->nb_channels;
- avctx->bit_rate = s->bit_rate;
- switch(s->layer) {
- case 1:
- avctx->frame_size = 384;
- break;
- case 2:
- avctx->frame_size = 1152;
- break;
- case 3:
- if (s->lsf)
- avctx->frame_size = 576;
- else
- avctx->frame_size = 1152;
- break;
- }
- }
- }
- } else if (s->frame_size == -1) {
- /* free format : find next sync to compute frame size */
- len = MPA_MAX_CODED_FRAME_SIZE - len;
- if (len > buf_size)
- len = buf_size;
- if (len == 0) {
- /* frame too long: resync */
- s->frame_size = 0;
- memmove(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1);
- s->inbuf_ptr--;
- } else {
- uint8_t *p, *pend;
- uint32_t header1;
- int padding;
-
- memcpy(s->inbuf_ptr, buf_ptr, len);
- /* check for header */
- p = s->inbuf_ptr - 3;
- pend = s->inbuf_ptr + len - 4;
- while (p <= pend) {
- header = (p[0] << 24) | (p[1] << 16) |
- (p[2] << 8) | p[3];
- header1 = (s->inbuf[0] << 24) | (s->inbuf[1] << 16) |
- (s->inbuf[2] << 8) | s->inbuf[3];
- /* check with high probability that we have a
- valid header */
- if ((header & SAME_HEADER_MASK) ==
- (header1 & SAME_HEADER_MASK)) {
- /* header found: update pointers */
- len = (p + 4) - s->inbuf_ptr;
- buf_ptr += len;
- buf_size -= len;
- s->inbuf_ptr = p;
- /* compute frame size */
- s->free_format_next_header = header;
- s->free_format_frame_size = s->inbuf_ptr - s->inbuf;
- padding = (header1 >> 9) & 1;
- if (s->layer == 1)
- s->free_format_frame_size -= padding * 4;
- else
- s->free_format_frame_size -= padding;
- dprintf("free frame size=%d padding=%d\n",
- s->free_format_frame_size, padding);
- decode_header(s, header1);
- goto next_data;
- }
- p++;
+ OUT_INT *out_samples = data;
+
+ len = buf_size;
+
+ // Discard too short frames
+ if (buf_size < HEADER_SIZE) {
+ *data_size = 0;
+ return buf_size;
+ }
+
+
+ if (len > MPA_MAX_CODED_FRAME_SIZE)
+ len = MPA_MAX_CODED_FRAME_SIZE;
+
+ // Get header and restore sync word
+ header = (buf[0] << 24) | (buf[1] << 16) | (buf[2] << 8) | buf[3] | 0xffe00000;
+
+ if (ff_mpa_check_header(header) < 0) { // Bad header, discard frame
+ *data_size = 0;
+ return buf_size;
+ }
+
+ decode_header(s, header);
+ /* update codec info */
+ avctx->sample_rate = s->sample_rate;
+ avctx->channels = s->nb_channels;
+ avctx->bit_rate = s->bit_rate;
+ avctx->sub_id = s->layer;
+
+ avctx->frame_size=s->frame_size = len;
+
+ if (avctx->parse_only) {
+ out_size = buf_size;
+ } else {
+ out_size = mp_decode_frame(s, out_samples, buf, buf_size);
+ }
+
+ *data_size = out_size;
+ return buf_size;
+}
+#endif /* CONFIG_MP3ADU_DECODER */
+
+#ifdef CONFIG_MP3ON4_DECODER
+/* Next 3 arrays are indexed by channel config number (passed via codecdata) */
+static int mp3Frames[16] = {0,1,1,2,3,3,4,5,2}; /* number of mp3 decoder instances */
+static int mp3Channels[16] = {0,1,2,3,4,5,6,8,4}; /* total output channels */
+/* offsets into output buffer, assume output order is FL FR BL BR C LFE */
+static int chan_offset[9][5] = {
+ {0},
+ {0}, // C
+ {0}, // FLR
+ {2,0}, // C FLR
+ {2,0,3}, // C FLR BS
+ {4,0,2}, // C FLR BLRS
+ {4,0,2,5}, // C FLR BLRS LFE
+ {4,0,2,6,5}, // C FLR BLRS BLR LFE
+ {0,2} // FLR BLRS
+};
+
+
+static int decode_init_mp3on4(AVCodecContext * avctx)
+{
+ MP3On4DecodeContext *s = avctx->priv_data;
+ int i;
+
+ if ((avctx->extradata_size < 2) || (avctx->extradata == NULL)) {
+ av_log(avctx, AV_LOG_ERROR, "Codec extradata missing or too short.\n");
+ return -1;
+ }
+
+ s->chan_cfg = (((unsigned char *)avctx->extradata)[1] >> 3) & 0x0f;
+ s->frames = mp3Frames[s->chan_cfg];
+ if(!s->frames) {
+ av_log(avctx, AV_LOG_ERROR, "Invalid channel config number.\n");
+ return -1;
+ }
+ avctx->channels = mp3Channels[s->chan_cfg];
+
+ /* Init the first mp3 decoder in standard way, so that all tables get builded
+ * We replace avctx->priv_data with the context of the first decoder so that
+ * decode_init() does not have to be changed.
+ * Other decoders will be inited here copying data from the first context
+ */
+ // Allocate zeroed memory for the first decoder context
+ s->mp3decctx[0] = av_mallocz(sizeof(MPADecodeContext));
+ // Put decoder context in place to make init_decode() happy
+ avctx->priv_data = s->mp3decctx[0];
+ decode_init(avctx);
+ // Restore mp3on4 context pointer
+ avctx->priv_data = s;
+ s->mp3decctx[0]->adu_mode = 1; // Set adu mode
+
+ /* Create a separate codec/context for each frame (first is already ok).
+ * Each frame is 1 or 2 channels - up to 5 frames allowed
+ */
+ for (i = 1; i < s->frames; i++) {
+ s->mp3decctx[i] = av_mallocz(sizeof(MPADecodeContext));
+ s->mp3decctx[i]->compute_antialias = s->mp3decctx[0]->compute_antialias;
+ s->mp3decctx[i]->adu_mode = 1;
+ }
+
+ return 0;
+}
+
+
+static int decode_close_mp3on4(AVCodecContext * avctx)
+{
+ MP3On4DecodeContext *s = avctx->priv_data;
+ int i;
+
+ for (i = 0; i < s->frames; i++)
+ if (s->mp3decctx[i])
+ av_free(s->mp3decctx[i]);
+
+ return 0;
+}
+
+
+static int decode_frame_mp3on4(AVCodecContext * avctx,
+ void *data, int *data_size,
+ uint8_t * buf, int buf_size)
+{
+ MP3On4DecodeContext *s = avctx->priv_data;
+ MPADecodeContext *m;
+ int len, out_size = 0;
+ uint32_t header;
+ OUT_INT *out_samples = data;
+ OUT_INT decoded_buf[MPA_FRAME_SIZE * MPA_MAX_CHANNELS];
+ OUT_INT *outptr, *bp;
+ int fsize;
+ unsigned char *start2 = buf, *start;
+ int fr, i, j, n;
+ int off = avctx->channels;
+ int *coff = chan_offset[s->chan_cfg];
+
+ len = buf_size;
+
+ // Discard too short frames
+ if (buf_size < HEADER_SIZE) {
+ *data_size = 0;
+ return buf_size;
+ }
+
+ // If only one decoder interleave is not needed
+ outptr = s->frames == 1 ? out_samples : decoded_buf;
+
+ for (fr = 0; fr < s->frames; fr++) {
+ start = start2;
+ fsize = (start[0] << 4) | (start[1] >> 4);
+ start2 += fsize;
+ if (fsize > len)
+ fsize = len;
+ len -= fsize;
+ if (fsize > MPA_MAX_CODED_FRAME_SIZE)
+ fsize = MPA_MAX_CODED_FRAME_SIZE;
+ m = s->mp3decctx[fr];
+ assert (m != NULL);
+
+ // Get header
+ header = (start[0] << 24) | (start[1] << 16) | (start[2] << 8) | start[3] | 0xfff00000;
+
+ if (ff_mpa_check_header(header) < 0) { // Bad header, discard block
+ *data_size = 0;
+ return buf_size;
+ }
+
+ decode_header(m, header);
+ mp_decode_frame(m, decoded_buf, start, fsize);
+
+ n = MPA_FRAME_SIZE * m->nb_channels;
+ out_size += n * sizeof(OUT_INT);
+ if(s->frames > 1) {
+ /* interleave output data */
+ bp = out_samples + coff[fr];
+ if(m->nb_channels == 1) {
+ for(j = 0; j < n; j++) {
+ *bp = decoded_buf[j];
+ bp += off;