shithub: rtmp

ref: 93f645314cceaf9fd5176cb0cbec475a91a843e2
dir: /adts.c/

View raw version
#include <u.h>
#include <libc.h>
#include <bio.h>
#include "adts.h"
#include "util.h"

static int ratecfg[] = {
	96000,
	88200,
	64000,
	48000,
	44100,
	32000,
	24000,
	22050,
	16000,
	12000,
	11025,
	8000,
	7350,
	-1,
	-1,
	-1,
};

int
adtsread(Biobuf *b, ADTSFrame *f)
{
	u8int h[7];
	int ratei;

	if(Bread(b, h, 7) != 7)
		goto err;
	if(h[0] != 0xff || (h[1]&0xf0) != 0xf0){
		werrstr("bad sync word");
		goto err;
	}

	f->sz = h[4]<<3 | h[5]>>5;
	if(f->sz < 7){
		werrstr("frame too small (%d bytes)", f->sz);
		goto err;
	}
	if(f->sz > f->bufsz){
		f->bufsz = f->sz*2;
		f->buf = erealloc(f->buf, f->bufsz);
	}

	memmove(f->buf, h, 7);
	if(Bread(b, f->buf+7, f->sz-7) != f->sz-7)
		goto err;

	ratei = (h[2]>>2) & 7;

	f->ns += 1000000000ULL * 1024ULL / ratecfg[ratei];

	return 0;

err:
	werrstr("adtsread: %r");
	return -1;
}