Beispiel #1
0
  public static PMT parsePMT(ByteBuffer data) {
    parseSection(data);

    // PMT itself
    int w1 = data.getShort() & 0xffff;
    int pcrPid = w1 & 0x1fff;

    int w2 = data.getShort() & 0xffff;
    int programInfoLength = w2 & 0xfff;

    List<Tag> tags = parseTags(NIOUtils.read(data, programInfoLength));
    List<PMTStream> streams = new ArrayList<PMTStream>();
    while (data.remaining() > 4) {
      int streamType = data.get() & 0xff;
      int wn = data.getShort() & 0xffff;
      int elementaryPid = wn & 0x1fff;

      Logger.info(String.format("Elementary stream: [%d,%d]", streamType, elementaryPid));

      int wn1 = data.getShort() & 0xffff;
      int esInfoLength = wn1 & 0xfff;
      ByteBuffer read = NIOUtils.read(data, esInfoLength);
      streams.add(new PMTStream(streamType, elementaryPid, MPSUtils.parseDescriptors(read)));
    }

    return new PMT(pcrPid, tags, streams);
  }
Beispiel #2
0
    public ByteBuffer transcodeFrame(ByteBuffer src, ByteBuffer dst) throws IOException {
      if (src == null) return null;
      Picture8Bit decoded = decoder.decodeFrame8Bit(src, pic0.getData());
      if (pic1 == null) {
        pic1 =
            Picture8Bit.create(
                decoded.getWidth(), decoded.getHeight(), encoder.getSupportedColorSpaces()[0]);
        transform =
            ColorUtil.getTransform8Bit(decoded.getColor(), encoder.getSupportedColorSpaces()[0]);
      }
      transform.transform(decoded, pic1);
      pic1.setCrop(new Rect(0, 0, track.thumbWidth, track.thumbHeight));
      int rate = TARGET_RATE;
      do {
        try {
          encoder.encodeFrame8Bit(pic1, dst);
          break;
        } catch (BufferOverflowException ex) {
          Logger.warn("Abandon frame, buffer too small: " + dst.capacity());
          rate -= 10;
          rc.setRate(rate);
        }
      } while (rate > 10);
      rc.setRate(TARGET_RATE);

      H264Utils.encodeMOVPacket(dst);

      return dst;
    }
Beispiel #3
0
 private static List<Tag> parseTags(ByteBuffer bb) {
   List<Tag> tags = new ArrayList<Tag>();
   while (bb.hasRemaining()) {
     int tag = bb.get();
     int tagLen = bb.get();
     Logger.info(String.format("TAG: [0x%x, 0x%x]", tag, tagLen));
     tags.add(new Tag(tag, NIOUtils.read(bb, tagLen)));
   }
   return tags;
 }
Beispiel #4
0
  public static Header read(ByteBuffer input) {
    long size = 0;
    while (input.remaining() >= 4 && (size = (((long) input.getInt()) & 0xffffffffL)) == 0) ;
    if (input.remaining() < 4 || size < 8 && size != 1) {
      Logger.error("Broken atom of size " + size);
      return null;
    }

    String fourcc = NIOUtils.readString(input, 4);
    boolean lng = false;
    if (size == 1) {
      if (input.remaining() >= 8) {
        lng = true;
        size = input.getLong();
      } else {
        Logger.error("Broken atom of size " + size);
        return null;
      }
    }

    return new Header(fourcc, size, lng);
  }
Beispiel #5
0
 protected void pes(ByteBuffer pesBuffer, long start, int pesLen, int stream) {
   if (!mediaStream(stream)) return;
   Logger.debug(String.format("PES: %08x, %d", start, pesLen));
   PESPacket pesHeader = readPESHeader(pesBuffer, start);
   int leadingTsPkt = 0; // pesBuffer.position();
   if (predFileStartInTsPkt != start) {
     leadingTsPkt = (int) (start / 188 - predFileStartInTsPkt);
   }
   predFileStartInTsPkt = (start + pesLen) / 188;
   int tsPktInPes = (int) (predFileStartInTsPkt - start / 188);
   savePESMeta(stream, MPSIndex.makePESToken(leadingTsPkt, tsPktInPes, pesBuffer.remaining()));
   getAnalyser(stream).pkt(pesBuffer, pesHeader);
 }
Beispiel #6
0
    public Frame decodeFrame(List<ByteBuffer> nalUnits, byte[][] buffer) {
      Frame result = null;

      for (ByteBuffer nalUnit : nalUnits) {
        NALUnit marker = NALUnit.read(nalUnit);

        unescapeNAL(nalUnit);

        switch (marker.type) {
          case NON_IDR_SLICE:
          case IDR_SLICE:
            if (result == null) {
              if (sps.size() == 0 || pps.size() == 0) {
                Logger.warn("Skipping frame as no SPS/PPS have been seen so far...");
                return null;
              }
              result = init(buffer, nalUnit, marker);
            }
            decoder.decode(nalUnit, marker);
            break;
          case SPS:
            SeqParameterSet _sps = SeqParameterSet.read(nalUnit);
            sps.put(_sps.seq_parameter_set_id, _sps);
            break;
          case PPS:
            PictureParameterSet _pps = PictureParameterSet.read(nalUnit);
            pps.put(_pps.pic_parameter_set_id, _pps);
            break;
          default:
        }
      }

      filter.deblockFrame(result);

      updateReferences(result);

      return result;
    }
  protected void read(Map<Integer, ByteBuffer> tags) {
    super.read(tags);

    for (Iterator<Entry<Integer, ByteBuffer>> it = tags.entrySet().iterator(); it.hasNext(); ) {
      Entry<Integer, ByteBuffer> entry = it.next();

      ByteBuffer _bb = entry.getValue();

      switch (entry.getKey()) {
        case 0x3215:
          signalStandard = _bb.get();
          break;
        case 0x320c:
          frameLayout = LayoutType.values()[_bb.get()];
          break;
        case 0x3203:
          storedWidth = _bb.getInt();
          break;
        case 0x3202:
          storedHeight = _bb.getInt();
          break;
        case 0x3216:
          storedF2Offset = _bb.getInt();
          break;
        case 0x3205:
          sampledWidth = _bb.getInt();
          break;
        case 0x3204:
          sampledHeight = _bb.getInt();
          break;
        case 0x3206:
          sampledXOffset = _bb.getInt();
          break;
        case 0x3207:
          sampledYOffset = _bb.getInt();
          break;
        case 0x3208:
          displayHeight = _bb.getInt();
          break;
        case 0x3209:
          displayWidth = _bb.getInt();
          break;
        case 0x320a:
          displayXOffset = _bb.getInt();
          break;
        case 0x320b:
          displayYOffset = _bb.getInt();
          break;
        case 0x3217:
          displayF2Offset = _bb.getInt();
          break;
        case 0x320e:
          aspectRatio = new Rational(_bb.getInt(), _bb.getInt());
          break;
        case 0x3218:
          activeFormatDescriptor = _bb.get();
          break;
        case 0x320d:
          videoLineMap = readInt32Batch(_bb);
          break;
        case 0x320f:
          alphaTransparency = _bb.get();
          break;
        case 0x3210:
          transferCharacteristic = UL.read(_bb);
          break;
        case 0x3211:
          imageAlignmentOffset = _bb.getInt();
          break;
        case 0x3213:
          imageStartOffset = _bb.getInt();
          break;
        case 0x3214:
          imageEndOffset = _bb.getInt();
          break;
        case 0x3212:
          fieldDominance = _bb.get();
          break;
        case 0x3201:
          pictureEssenceCoding = UL.read(_bb);
          break;
        case 0x321a:
          codingEquations = UL.read(_bb);
          break;
        case 0x3219:
          colorPrimaries = UL.read(_bb);
          break;
        default:
          Logger.warn(String.format("Unknown tag [ " + ul + "]: %04x", entry.getKey()));
          continue;
      }
      it.remove();
    }
  }