Esempio n. 1
0
static int mp3_read_packet(AVFormatContext *s, AVPacket *pkt)
{
    int ret, size;
	//    AVStream *st = s->streams[0];
	uint32_t header = 0;
	int64_t oldseek = url_fseek(s->pb, 0, SEEK_CUR);
	int res;
	MPADecodeContext s_temp;

	header = (get_byte(s->pb) << 24 & 0xff000000) + (get_byte(s->pb) << 16 & 0x00ff0000) 
		+ (get_byte(s->pb) << 8 & 0x0000ff00) + get_byte(s->pb);
	res = url_fseek(s->pb, oldseek, SEEK_SET);
	if(ff_mpegaudio_decode_header(&s_temp, header) == 1) {
		size = MP3_PACKET_SIZE;
	} else {
		size = s_temp.frame_size;
	}
	
//	size= 626;//MP3_PACKET_SIZE;
	
    ret= av_get_packet(s->pb, pkt, size);

    pkt->stream_index = 0;
    if (ret <= 0) {
        return AVERROR(EIO);
    }
    /* note: we need to modify the packet size here to handle the last
       packet */
    pkt->size = ret;
    return ret;
}
Esempio n. 2
0
/**
 * Try to find Xing/Info/VBRI tags and compute duration from info therein
 */
static int mp3_parse_vbr_tags(AVFormatContext *s, AVStream *st, int64_t base)
{
    uint32_t v, spf;
    unsigned frames = 0; /* Total number of frames in file */
    unsigned size = 0; /* Total number of bytes in the stream */
    const int64_t xing_offtbl[2][2] = {{32, 17}, {17,9}};
    MPADecodeHeader c;
    int vbrtag_size = 0;

    v = avio_rb32(s->pb);
    if(ff_mpa_check_header(v) < 0)
      return -1;

    if (ff_mpegaudio_decode_header(&c, v) == 0)
        vbrtag_size = c.frame_size;
    if(c.layer != 3)
        return -1;

    /* Check for Xing / Info tag */
    url_fseek(s->pb, xing_offtbl[c.lsf == 1][c.nb_channels == 1], SEEK_CUR);
    v = avio_rb32(s->pb);
    if(v == MKBETAG('X', 'i', 'n', 'g') || v == MKBETAG('I', 'n', 'f', 'o')) {
        v = avio_rb32(s->pb);
        if(v & 0x1)
            frames = avio_rb32(s->pb);
        if(v & 0x2)
            size = avio_rb32(s->pb);
    }

    /* Check for VBRI tag (always 32 bytes after end of mpegaudio header) */
    url_fseek(s->pb, base + 4 + 32, SEEK_SET);
    v = avio_rb32(s->pb);
    if(v == MKBETAG('V', 'B', 'R', 'I')) {
        /* Check tag version */
        if(avio_rb16(s->pb) == 1) {
            /* skip delay and quality */
            url_fseek(s->pb, 4, SEEK_CUR);
            frames = avio_rb32(s->pb);
            size = avio_rb32(s->pb);
        }
    }

    if(!frames && !size)
        return -1;

    /* Skip the vbr tag frame */
    url_fseek(s->pb, base + vbrtag_size, SEEK_SET);

    spf = c.lsf ? 576 : 1152; /* Samples per frame, layer 3 */
    if(frames)
        st->duration = av_rescale_q(frames, (AVRational){spf, c.sample_rate},
                                    st->time_base);
    if(size && frames)
        st->codec->bit_rate = av_rescale(size, 8 * c.sample_rate, frames * (int64_t)spf);

    return 0;
}
Esempio n. 3
0
/**
 * Try to find Xing/Info/VBRI tags and compute duration from info therein
 */
static void mp3_parse_vbr_tags(AVFormatContext *s, AVStream *st, int64_t base)
{
    uint32_t v, spf;
    int frames = -1; /* Total number of frames in file */
    const int64_t xing_offtbl[2][2] = {{32, 17}, {17,9}};
    MPADecodeContext c;
	AVRational temp;
    v = get_be32(s->pb);
    if(ff_mpa_check_header(v) < 0)
      return;

    ff_mpegaudio_decode_header(&c, v);
    if(c.layer != 3)
        return;

    /* Check for Xing / Info tag */
    url_fseek(s->pb, xing_offtbl[c.lsf == 1][c.nb_channels == 1], SEEK_CUR);
    v = get_be32(s->pb);
    if(v == MKBETAG('X', 'i', 'n', 'g') || v == MKBETAG('I', 'n', 'f', 'o')) {
        v = get_be32(s->pb);
        if(v & 0x1)
            frames = get_be32(s->pb);
    }

    /* Check for VBRI tag (always 32 bytes after end of mpegaudio header) */
    url_fseek(s->pb, base + 4 + 32, SEEK_SET);
    v = get_be32(s->pb);
    if(v == MKBETAG('V', 'B', 'R', 'I')) {
        /* Check tag version */
        if(get_be16(s->pb) == 1) {
            /* skip delay, quality and total bytes */
            url_fseek(s->pb, 8, SEEK_CUR);
            frames = get_be32(s->pb);
        }
    }

    if(frames < 0)
        return;

    spf = c.lsf ? 576 : 1152; /* Samples per frame, layer 3 */
    temp.num = spf; temp.den = c.sample_rate;
	st->duration = av_rescale_q(frames, temp,
                                st->time_base);
}
/* useful helper to get mpeg audio stream infos. Return -1 if error in
   header, otherwise the coded frame size in bytes */
int ff_mpa_decode_header(AVCodecContext *avctx, uint32_t head, int *sample_rate, int *channels, int *frame_size, int *bit_rate)
{
	MPADecodeHeader s1, *s = &s1;

	if (ff_mpa_check_header(head) != 0)
		return -1;

	if (ff_mpegaudio_decode_header(s, head) != 0)
	{
		return -1;
	}

	switch(s->layer)
	{
	case 1:
		avctx->codec_id = CODEC_ID_MP1;
		*frame_size = 384;
		break;
	case 2:
		avctx->codec_id = CODEC_ID_MP2;
		*frame_size = 1152;
		break;
	default:
	case 3:
		avctx->codec_id = CODEC_ID_MP3;
		if (s->lsf)
			*frame_size = 576;
		else
			*frame_size = 1152;
		break;
	}

	*sample_rate = s->sample_rate;
	*channels = s->nb_channels;
	*bit_rate = s->bit_rate;
	avctx->sub_id = s->layer;
	return s->frame_size;
}