📄 mpegaudiodec.c
字号:
{ lsf_sf_expand(slen, sf, 5, 4, 4); tindex2 = 0; } else if (sf < 500) { lsf_sf_expand(slen, sf - 400, 5, 4, 0); tindex2 = 1; } else { lsf_sf_expand(slen, sf - 500, 3, 0, 0); tindex2 = 2; g->preflag = 1; } } j = 0; for (k = 0; k < 4; k++) { n = lsf_nsf_table[tindex2][tindex][k]; sl = slen[k]; for (i = 0; i < n; i++) g->scale_factors[j++] = get_bitsz(&s->gb, sl); } /* XXX: should compute exact size */ for (; j < 40; j++) g->scale_factors[j] = 0;#if defined(DEBUG) { printf("gr=%d ch=%d scale_factors:\n", gr, ch); for (i = 0; i < 40; i++) printf(" %d", g->scale_factors[i]); printf("\n"); }#endif } exponents_from_scale_factors(s, g, exponents); // does not helpmemset(g->sb_hybrid, 0, 576 * 4); // sb_hybrid is filled by huffman_decode /* read Huffman coded residue */ if (huffman_decode(s, g, exponents, bits_pos + g->part2_3_length) < 0) return -1; //debug_dump(g->sb_hybrid, 576, "after huffman_decode ");#if defined(DEBUG) sample_dump(0, g->sb_hybrid, 576);#endif /* skip extension bits */ bits_left = g->part2_3_length - (get_bits_count(&s->gb) - bits_pos); if (bits_left < 0) { dprintf("bits_left=%d\n", bits_left); return - 1; } while (bits_left >= 16) { skip_bits(&s->gb, 16); bits_left -= 16; } if (bits_left > 0) skip_bits(&s->gb, bits_left); } /* ch */ //debug_dump(&granules[0][gr].part2_3_length, sizeof(struct GranuleDef)>>2, "ch0 "); //debug_dump(&granules[1][gr].part2_3_length, sizeof(struct GranuleDef)>>2, "ch1 "); if (s->nb_channels == 2) compute_stereo(s, &granules[0][gr], &granules[1][gr]); for (ch = 0; ch < s->nb_channels; ch++) { g = &granules[ch][gr]; reorder_block(s, g);#if defined(DEBUG) sample_dump(0, g->sb_hybrid, 576);#endif compute_antialias(s, g);#if defined(DEBUG) sample_dump(1, g->sb_hybrid, 576);#endif compute_imdct(s, g, &s->sb_samples[ch][18 *gr][0], s->mdct_buf[ch]);#if defined(DEBUG) sample_dump(2, &s->sb_samples[ch][18 *gr][0], 576);#endif } } /* gr */ return nb_granules * 18;}static int mp_decode_frame(MPADecodeContext *s, short *samples){ int i, nb_frames, ch; short *samples_ptr; init_get_bits(&s->gb, s->inbuf + HEADER_SIZE, (s->inbuf_ptr - s->inbuf - HEADER_SIZE) * 8); /* skip error protection field */ if (s->error_protection) get_bits(&s->gb, 16); dprintf("frame %d:\n", s->frame_count); switch (s->layer) { case 1: nb_frames = mp_decode_layer1(s); break; case 2: nb_frames = mp_decode_layer2(s); break; case 3: default: nb_frames = mp_decode_layer3(s); break; } /* apply the synthesis filter */ for (ch = 0; ch < s->nb_channels; ch++) { samples_ptr = samples + ch; for (i = 0; i < nb_frames; i++) { int tmp[32]; dct32(tmp, s->sb_samples[ch][i]); synth_filter(s->synth_buf[ch] + s->synth_buf_offset[ch], &s->synth_buf_offset[ch], samples_ptr, s->nb_channels, tmp); samples_ptr += 32 * s->nb_channels; } } s->frame_count++; return nb_frames * 32 * sizeof(short) * s->nb_channels;}static int ignore_count;static int ramp, rampinc, normal_bufsize;#define RAMP_MAX 16776960void init_ignore_buffer_count(int rate, int channels, int absolute){ HEADER_PRINTF((M_TEXT("init_ignore_buffer_count rate %d, channels %x, absolute %x\n"), rate, channels, absolute)); if (absolute) ignore_count = absolute; else if (normal_bufsize > 0) { ignore_count = (rate / normal_bufsize) / 8; // printf("ignore_count %d\n", ignore_count); ramp = RAMP_MAX; rampinc = -((ramp / channels) / rate); // printf("normal_bufsize %d, rampinc %d\n", normal_bufsize, rampinc); }}static int decode_frame(MPEG_codecContext *avctx, void *data, int *data_size, uint8_t *buf, int buf_size){ MPADecodeContext *s = avctx->priv_data; uint32_t header; uint8_t *buf_ptr; int len, out_size; short *out_samples = data; int search_length = 0; *data_size = 0; buf_ptr = buf; while (buf_size > 0) { len = s->inbuf_ptr - s->inbuf; dprintf("buf_size %x %x %x\n", buf_size, len, s->frame_size); 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; } //HEADER_PRINTF((M_TEXT("no header seen : find one. We need at least HEADER_SIZE bytes to parse it\n"))); len = HEADER_SIZE - len; if (len > buf_size) { len = buf_size; } if (len > 0) { c_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) { search_length++; //HEADER_PRINTF((M_TEXT("no sync found : move by one byte (inefficient, but simple!) search_length %x\n"), search_length)); c_memcpy(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1); s->inbuf_ptr--; //HEADER_PRINTF((M_TEXT("skip header %x\n"), header)); dprintf("reset free format frame size to give a chance to get a new bitrate\n"); s->free_format_frame_size = 0; } else { if (search_length > 25) /* somethng bad has happened to the bit stream so aviod noises */ { HEADER_PRINTF((M_TEXT("use header %x, search_length %x\n"), header, search_length)); init_ignore_buffer_count(s->sample_rate, s->nb_channels, 3); /* prevent playing chirps in the immediate future */ } if (decode_header(s, header) == 1) { /* free format: prepare to compute frame size */ s->frame_size = -1; } /* update codec info */ 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) { dprintf("free format : find next sync to compute frame size\n"); 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; c_memcpy(s->inbuf, s->inbuf + 1, s->inbuf_ptr - s->inbuf - 1); s->inbuf_ptr--; } else { uint8_t *p, *pend; uint32_t header1; int padding; c_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++; } /* not found: simply increase pointers */ buf_ptr += len; s->inbuf_ptr += len; buf_size -= len; } } else if (len < s->frame_size) { if (s->frame_size > MPA_MAX_CODED_FRAME_SIZE) s->frame_size = MPA_MAX_CODED_FRAME_SIZE; len = s->frame_size - len; if (len > buf_size) len = buf_size; c_memcpy(s->inbuf_ptr, buf_ptr, len); buf_ptr += len; s->inbuf_ptr += len; buf_size -= len; }next_data: if ((s->frame_size > 0) && ((s->inbuf_ptr - s->inbuf) >= s->frame_size)) { if (avctx->parse_only) { /* simply return the frame data */ *(uint8_t **)data = s->inbuf; out_size = s->inbuf_ptr - s->inbuf; } else { out_size = mp_decode_frame(s, out_samples); } /* To guard against glitches, clicks and chirps, we insist on N adjacent buffers having the same type, else no output */ { static int layer, srate, chans; // printf("layer%d, %d hz, %d bits/sec, framesize %d\n", s->layer, s->sample_rate, s->bit_rate, s->frame_size); // printf("layer %d %d, rate %d %d, channels %d %d\n", s->layer, layer, s->sample_rate, srate, chans, s->nb_channels); if ((s->layer != layer) || (s->sample_rate != srate) || (chans != s->nb_channels)) { layer = s->layer; srate = s->sample_rate; chans = s->nb_channels; ignore_count = 2; } else if (ignore_count > 0) /* prevent ignore_count from wrapping around */ ignore_count--; if (ignore_count > 0) { HEADER_PRINTF((M_TEXT("KILLING %d OUTPUT SAMPLES\n"), out_size)); if (out_size > 0) { if (out_size > (AVCODEC_MAX_AUDIO_FRAME_SIZE/sizeof(short))) out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE / sizeof(short); /* limit this in case we have a gross error */ memset(out_samples, 0, out_size * sizeof(short)); /* kill the noise */ } else out_size = 0; } } s->inbuf_ptr = s->inbuf; s->frame_size = 0; *data_size = out_size; if (out_size > 0) { if (ramp) { int ii; short *src = out_samples; short *dest = out_samples; int loop = (out_size / sizeof(short)); for (ii = 0; ii < loop; ii++) { dest[ii] = (short)(((int)((RAMP_MAX - ramp) >> 8) * (int)src[ii]) >> 16); ramp += rampinc; if (ramp < 0) ramp = 0; } } normal_bufsize = ((out_size / 2) / s->nb_channels); } break; } } //printf("used %d\n", buf_ptr - buf); return buf_ptr - buf;}static int tagsize;static int gobble_amount;/* decode an audio frame. return -1 if error, otherwise return the *number of bytes used. If no frame could be decompressed, *frame_size_ptr is zero. Otherwise, it is the decompressed frame *size in BYTES. */int avcodec_decode_audio(MPEG_codecContext *avctx, int16_t *samples, int *frame_size_ptr, uint8_t *input_buf, int inbuf_size){ int ret; tagsize = 0; if (!memcmp(input_buf, "ID3", 3)) { /* high bit is not used */ tagsize = (input_buf[6] << 21) | (input_buf[7] << 14) | (input_buf[8] << 7) | (input_buf[9] << 0); tagsize += 10; INFO_PRINTF((M_TEXT("found ID3 tag %d bytes long\n"), tagsize)); /* guard against unreasonably long ID3 tags */ if (tagsize > (3 * 65536)) /* assume that it is not really an ID3 tag, but an error instead */ { tagsize = 0; return inbuf_size; /* gobble this buffer but no others */ } DPRINTF((M_TEXT("gobbling %d, inbuf_size %d\n"), tagsize, inbuf_size)); if (inbuf_size <= tagsize) { *frame_size_ptr = 0; gobble_amount = tagsize - inbuf_size; DPRINTF((M_TEXT("gobble left %d\n"), gobble_amount)); return inbuf_size; } } if (gobble_amount) { if (inbuf_size <= gobble_amount) { *frame_size_ptr = 0; gobble_amount = gobble_amount - inbuf_size; DPRINTF((M_TEXT("gobble left %d\n"), gobble_amount)); return inbuf_size; } else { tagsize = gobble_amount; gobble_amount = 0; } } ret = decode_frame(avctx, samples, frame_size_ptr, input_buf + tagsize, inbuf_size - tagsize) + tagsize; frame_number++; return ret;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -