www.pudn.com > eval-1.2.zip > mpeg-4.c



#include 

#include "misc.h"
#include "mpeg-4.h"

static unsigned bit_ = 8, pos_;

void resetbuf()
{
  pos_ = 0;
  bit_ = 8;
}

int getsc(char *s, unsigned l)
{
  unsigned i = 0;
  unsigned char c, zeros = 0;

  while (i < l) {
    c = nextbits(s, 8);

    if (c == 0) {
      if (zeros < 2) zeros++;
    } else {
      if (c == 1 && zeros == 2) {
        c = nextbits(s, 8);
        bit_ = 0;
        return c;
      }
      zeros = 0;
    }
  }

  return -1;
}

int checksc(int sc)
{
  if (sc == VOS_SC) return VOS_SC;
  if (sc == VO_SC) return VO_SC;
  if (sc >= VDO_SC_MIN && sc <= VDO_SC_MAX) return VDO_SC;
  if (sc >= VOL_SC_MIN && sc <= VOL_SC_MAX) return VOL_SC;
  if (sc >= SYS_SC_MIN && sc <= SYS_SC_MAX) return SYS_SC;
  if (sc >= RES_SC_MIN && sc <= RES_SC_MAX) return RES_SC;
  if (sc >= 0xc3 && sc <= 0xc5) return RES_SC;

  return -1;
}

unsigned nextbits(char *s, unsigned n)
{
  unsigned i, v = 0;

  for (i=0; i 1) return 0;

  for (;;) {
    if (!bit_) pos_++, bit_ = 8;
    if (b == !!(s[pos_] & 1U << --bit_)) count++; else break;
  }

  return count;
}

int VOLheader(char *s, unsigned l, VOL_header *VOL_h)
{
  int sc;

  while (-1 != (sc = checksc(getsc(s, l)))) {
    if (sc == VOL_SC) {
      nextbits(s, 1);
      VOL_h->type = nextbits(s, 8);
      if (nextbits(s, 1)) {
        VOL_h->verid = nextbits(s, 4);
        VOL_h->priority = nextbits(s, 3);
      }
      if ((VOL_h->aspect = nextbits(s, 4)) == 15) {
        VOL_h->width = nextbits(s, 8);
        VOL_h->height = nextbits(s, 8);
      }
      if (nextbits(s, 1)) {
        nextbits(s, 2);
        nextbits(s, 1);
        if (nextbits(s, 1)) nextbits(s, 63);
      }
      if ((VOL_h->shape = nextbits(s, 2)) == 3)
        VOL_h->shape_ex = nextbits(s, 4);
      if (!nextbits(s, 1)) goto E;
      VOL_h->vop_time_inc_res = nextbits(s, 16);
      if (!nextbits(s, 1)) goto E;
      if(nextbits(s, 1))
        VOL_h->fixed_vop_time_inc = nextbits(s, CalcMinBits(VOL_h->vop_time_inc_res));
      if (!VOL_h->shape) {
        if (!nextbits(s, 1)) goto E;
        VOL_h->width = nextbits(s, 13);
        if (!nextbits(s, 1)) goto E;
        VOL_h->height = nextbits(s, 13);
        if (!nextbits(s, 1)) goto E;
      }
      return 1;
    }
  }

E:
  fprintf(stderr, "MPEG-4 header parsing failed\n");
  return 0;
}

int VOP_not_coded(char *s, VOL_header *VOL_h)
{
  if (nextbits(s, 8) != 0) goto E;
  if (nextbits(s, 8) != 0) goto E;
  if (nextbits(s, 8) != 1) goto E;
  if (nextbits(s, 8) != 0xB6) goto E;                /* VOL start code */

  nextbits(s, 2);                                    /* VOP type */
  skipbits(s, 1);                                    /* modulo_time_base */
  if (!nextbits(s, 1)) goto E;                       /* marker bit */
  nextbits(s, CalcMinBits(VOL_h->vop_time_inc_res)); /* vop_time_increment */
  if (!nextbits(s, 1)) goto E;                       /* marker bit */
  CLRBIT(s[pos_], bit_ - 1);                         /* set VOP not coded */
  if (nextbits(s, 1)) goto E;                        /* VOP coded ? */

  return pos_;

E:
  fprintf(stderr, "frame parsing error!\n");
  return 0;
}