int main(int argc, char *argv[]) {
    /*perform simple round-trip using page reader and writer*/

    BitstreamReader *reader = br_open(stdin, BS_LITTLE_ENDIAN);
    BitstreamWriter *writer = bw_open(stdout, BS_LITTLE_ENDIAN);
    struct ogg_page page;

    do {
        ogg_status result;
        if ((result = read_ogg_page(reader, &page)) == OGG_OK) {
            write_ogg_page(writer, &page);
        } else {
            fprintf(stderr, "*** Error: %s", ogg_strerror(result));
            goto error;
        }
    } while (!page.header.stream_end);

    reader->close(reader);
    writer->close(writer);
    return 0;

error:
    reader->close(reader);
    writer->close(writer);
    return 1;
}
Example #2
0
SubLayerHrdParameters HevcNalDecode::processSubLayerHrdParameters(uint8_t sub_pic_hrd_params_present_flag, std::size_t CpbCnt, BitstreamReader &bs)
{
  SubLayerHrdParameters slhrd;

  slhrd.toDefault();
  slhrd.bit_rate_value_minus1.resize(CpbCnt + 1);
  slhrd.cpb_size_value_minus1.resize(CpbCnt + 1);
  slhrd.cpb_size_du_value_minus1.resize(CpbCnt + 1);
  slhrd.bit_rate_du_value_minus1.resize(CpbCnt + 1);
  slhrd.cbr_flag.resize(CpbCnt + 1);

  for(std::size_t i=0; i<=CpbCnt; i++)
  {
    slhrd.bit_rate_value_minus1[i] = bs.getGolombU();
    slhrd.cpb_size_value_minus1[i] = bs.getGolombU();

    if(sub_pic_hrd_params_present_flag)
    {
      slhrd.cpb_size_du_value_minus1[i] = bs.getGolombU();
      slhrd.bit_rate_du_value_minus1[i] = bs.getGolombU();
    }

    slhrd.cbr_flag[i] = bs.getBits(1);
  }

  return slhrd;
}
Example #3
0
static status_t
decode_frameset(decoders_ALACDecoder *self,
                unsigned *pcm_frames_read,
                int *samples)
{
    BitstreamReader *br = self->bitstream;
    int channel_0[self->params.block_size];
    int channel_1[self->params.block_size];
    unsigned c = 0;
    unsigned block_size = self->params.block_size;
    unsigned channels = br->read(br, 3) + 1;

    while (channels != 8) {
        status_t status;
        unsigned frame_block_size;

        if ((channels != 1) && (channels != 2)) {
            /*only handle 1 or 2 channel frames*/
            return INVALID_FRAME_CHANNEL_COUNT;
        } else if ((c + channels) > self->channels) {
            /*ensure one doesn't decode too many channels*/
            return EXCESSIVE_FRAME_CHANNEL_COUNT;
        }

        if ((status = decode_frame(br,
                                   &(self->params),
                                   self->bits_per_sample,
                                   c == 0 ? &block_size : &frame_block_size,
                                   channels,
                                   channel_0,
                                   channel_1)) != OK) {
            return status;
        } else if ((c != 0) && (block_size != frame_block_size)) {
            return FRAME_BLOCK_SIZE_MISMATCH;
        }

        put_channel_data(samples,
                         c++,
                         self->channels,
                         block_size,
                         channel_0);

        if (channels == 2) {
            put_channel_data(samples,
                             c++,
                             self->channels,
                             block_size,
                             channel_1);
        }

        channels = br->read(br, 3) + 1;
    }
    br->byte_align(br);
    *pcm_frames_read = block_size;
    return OK;
}
Example #4
0
NALUnitType HevcNalDecode::processNALUnitHeader(BitstreamReader &bs)
{
  //forbidden_zero_bit
  bs.getBit();

  NALUnitType type = (NALUnitType)bs.getBits(6);

  //nuh_layer_id
  bs.getBits(6);

  //nuh_temporal_id_plus1
  bs.getBits(3);
  return type;
}
Example #5
0
ScalingListData HevcNalDecode::processScalingListData(BitstreamReader &bs)
{
  ScalingListData sc;

  sc.scaling_list_pred_mode_flag.resize(4);
  sc.scaling_list_pred_matrix_id_delta.resize(4);
  sc.scaling_list_dc_coef_minus8.resize(2);
  sc.scaling_list_delta_coef.resize(4);

  for(std::size_t sizeId = 0; sizeId < 4; sizeId++)
  {
    if(sizeId == 3)
    {
      sc.scaling_list_pred_mode_flag[sizeId].resize(2);
      sc.scaling_list_pred_matrix_id_delta[sizeId].resize(2);
      sc.scaling_list_dc_coef_minus8[sizeId-2].resize(2);
      sc.scaling_list_delta_coef[sizeId].resize(2);
    }
    else
    {
      sc.scaling_list_pred_mode_flag[sizeId].resize(6);
      sc.scaling_list_pred_matrix_id_delta[sizeId].resize(6);
      sc.scaling_list_delta_coef[sizeId].resize(6);
      if(sizeId >= 2)
        sc.scaling_list_dc_coef_minus8[sizeId-2].resize(6);
    }

    for(std::size_t matrixId = 0; matrixId<((sizeId == 3)?2:6); matrixId++)
    {
      sc.scaling_list_pred_mode_flag[sizeId][matrixId] = bs.getBits(1);
      if(!sc.scaling_list_pred_mode_flag[sizeId][matrixId])
        sc.scaling_list_pred_matrix_id_delta[sizeId][matrixId] = bs.getGolombU();
      else
      {
        std::size_t nextCoef = 8;
        std::size_t coefNum = std::min(64, (1 << (4 + (sizeId << 1))));
        if(sizeId > 1)
          sc.scaling_list_dc_coef_minus8[sizeId-2][matrixId] = bs.getGolombS();

        sc.scaling_list_delta_coef[sizeId][matrixId].resize(coefNum);
        for(std::size_t i = 0; i < coefNum; i++)
          sc.scaling_list_delta_coef[sizeId][matrixId][i] = bs.getGolombS();
      }
    }
  }

  return sc;
}
Example #6
0
int main(int argc, char *argv[]) {
    FILE *f = fopen(argv[1], "rb");
    OggReader *reader = oggreader_open(f);
    BitstreamReader *packet = bs_substream_new(BS_LITTLE_ENDIAN);
    ogg_status result;

    do {
        result = oggreader_next_packet(reader, packet);
        if (result < 0) {
            fprintf(stderr, "Error : %s\n", ogg_error(result));
        } else if (result == OGG_OK) {
            printf("packet size %u\n", packet->input.substream->buffer_size);
        }
    } while (result == OGG_OK);

    packet->close(packet);
    oggreader_close(reader);
    fclose(f);
    return 0;
}
static Status parseAPP14(DataReader *pReader, JPEGAPP14Header *pHeader)
{
    BitstreamReader bs;

    Ipp8u  iMarker[5];
    size_t iSize = 5;

    pReader->Get16uSwap(&pHeader->iLength);

    pReader->CacheData(&iMarker[0], iSize);
    if(iMarker[0] == 'a' && iMarker[1] == 'd' && iMarker[2] == 'o' && iMarker[3] == 'b' && iMarker[4] == 'e')
    {
        pReader->MovePosition(5);
        bs.Init(pReader);

        pHeader->bAdobeDetected  = true;
        pHeader->iAdobeVersion   = bs.GetBits(16);
        pHeader->iAdobeFlags0    = bs.GetBits(16);
        pHeader->iAdobeFlags1    = bs.GetBits(16);
        pHeader->iAdobeTransform = bs.GetBits(8);

        bs.AlignReader();
    }

    return UMC_OK;
}
Example #8
0
Status Mpeg2FrameConstructor::ParsePictureHeader(Ipp8u *buf, Ipp32s iLen, Mpeg2TrackInfo *pInfo) {
    BitstreamReader bs;
    bs.SkipBits(32 + 4 + 4 + 4 + 4 + 4 + 2);
    bs.SkipBits(1 + 1 + 1 + 1 + 1 + 1 + 1);
    bs.SkipBits(5);
    bs.SkipBits(3);
    Ipp8u source_format;
    bs.SkipBits(22);
    bs.SkipBits(8);
    if (7 == source_format)     {
	Ipp8u ufep = (Ipp8u)bs.GetBits(3);
	if (0x01 == ufep)         {
	    bs.SkipBits(10);
	}
    }
    return Status();
}
static Status parseSOF(DataReader *pReader, JPEGSOFHeader *pHeader)
{
    BitstreamReader bs;
    bs.Init(pReader);

    pHeader->iLength    = bs.GetBits(16);
    pHeader->iPrecision = bs.GetBits(8);
    pHeader->iHeight    = bs.GetBits(16);
    pHeader->iWidth     = bs.GetBits(16);
    pHeader->iComonents = bs.GetBits(8);

    for(Ipp32u i = 0; i < pHeader->iComonents; i++)
    {
        pHeader->comonent[i].iID        = bs.GetBits(8);
        pHeader->comonent[i].iHSampling = bs.GetBits(4);
        pHeader->comonent[i].iVSampling = bs.GetBits(4);
        pHeader->comonent[i].iQSelector = bs.GetBits(8);
    }
    bs.AlignReader();

    return UMC_OK;
}
Example #10
0
static PyObject*
ALACDecoder_read(decoders_ALACDecoder* self, PyObject *args)
{
    unsigned channel_count;
    BitstreamReader* mdat = self->bitstream;
    array_ia* frameset_channels = self->frameset_channels;
    PyThreadState *thread_state;

    /*return an empty framelist if total samples are exhausted*/
    if (self->remaining_frames == 0) {
        return empty_FrameList(self->audiotools_pcm,
                               self->channels,
                               self->bits_per_sample);
    }

    thread_state = PyEval_SaveThread();

    if (!setjmp(*br_try(mdat))) {
        frameset_channels->reset(frameset_channels);

        /*get initial frame's channel count*/
        channel_count = mdat->read(mdat, 3) + 1;
        while (channel_count != 8) {
            /*read a frame from the frameset into "channels"*/
            if (read_frame(self, mdat,
                           frameset_channels, channel_count) != OK) {
                br_etry(mdat);
                PyEval_RestoreThread(thread_state);
                PyErr_SetString(PyExc_ValueError, self->error_message);
                return NULL;
            } else {
                /*ensure all frames have the same sample count*/
                /*FIXME*/

                /*read the channel count of the next frame
                  in the frameset, if any*/
                channel_count = mdat->read(mdat, 3) + 1;
            }
        }

        /*once all the frames in the frameset are read,
          byte-align the output stream*/
        mdat->byte_align(mdat);
        br_etry(mdat);
        PyEval_RestoreThread(thread_state);

        /*decrement the remaining sample count*/
        self->remaining_frames -= MIN(self->remaining_frames,
                                      frameset_channels->_[0]->len);

        /*convert ALAC channel assignment to standard audiotools assignment*/
        alac_order_to_wave_order(frameset_channels);

        /*finally, build and return framelist object from the sample data*/
        return array_ia_to_FrameList(self->audiotools_pcm,
                                     frameset_channels,
                                     self->bits_per_sample);
    } else {
        br_etry(mdat);
        PyEval_RestoreThread(thread_state);
        PyErr_SetString(PyExc_IOError, "EOF during frame reading");
        return NULL;
    }
}
/// ParseBlock - Read a block, updating statistics, etc.
static bool ParseBlock(BitstreamReader &Stream, unsigned IndentLevel) {
  std::string Indent(IndentLevel*2, ' ');
  uint64_t BlockBitStart = Stream.GetCurrentBitNo();
  unsigned BlockID = Stream.ReadSubBlockID();

  // Get the statistics for this BlockID.
  PerBlockIDStats &BlockStats = BlockIDStats[BlockID];
  
  BlockStats.NumInstances++;
  
  // BLOCKINFO is a special part of the stream.
  if (BlockID == bitc::BLOCKINFO_BLOCK_ID) {
    if (Dump) std::cerr << Indent << "<BLOCKINFO_BLOCK/>\n";
    if (Stream.ReadBlockInfoBlock())
      return Error("Malformed BlockInfoBlock");
    uint64_t BlockBitEnd = Stream.GetCurrentBitNo();
    BlockStats.NumBits += BlockBitEnd-BlockBitStart;
    return false;
  }
  
  unsigned NumWords = 0;
  if (Stream.EnterSubBlock(BlockID, &NumWords))
    return Error("Malformed block record");

  const char *BlockName = 0;
  if (Dump) {
    std::cerr << Indent << "<";
    if ((BlockName = GetBlockName(BlockID)))
      std::cerr << BlockName;
    else
      std::cerr << "UnknownBlock" << BlockID;
    
    if (NonSymbolic && BlockName)
      std::cerr << " BlockID=" << BlockID;
    
    std::cerr << " NumWords=" << NumWords
              << " BlockCodeSize=" << Stream.GetAbbrevIDWidth() << ">\n";
  }
  
  SmallVector<uint64_t, 64> Record;

  // Read all the records for this block.
  while (1) {
    if (Stream.AtEndOfStream())
      return Error("Premature end of bitstream");

    // Read the code for this record.
    unsigned AbbrevID = Stream.ReadCode();
    switch (AbbrevID) {
    case bitc::END_BLOCK: {
      if (Stream.ReadBlockEnd())
        return Error("Error at end of block");
      uint64_t BlockBitEnd = Stream.GetCurrentBitNo();
      BlockStats.NumBits += BlockBitEnd-BlockBitStart;
      if (Dump) {
        std::cerr << Indent << "</";
        if (BlockName)
          std::cerr << BlockName << ">\n";
        else
          std::cerr << "UnknownBlock" << BlockID << ">\n";
      }
      return false;
    } 
    case bitc::ENTER_SUBBLOCK: {
      uint64_t SubBlockBitStart = Stream.GetCurrentBitNo();
      if (ParseBlock(Stream, IndentLevel+1))
        return true;
      ++BlockStats.NumSubBlocks;
      uint64_t SubBlockBitEnd = Stream.GetCurrentBitNo();
      
      // Don't include subblock sizes in the size of this block.
      BlockBitStart += SubBlockBitEnd-SubBlockBitStart;
      break;
    }
    case bitc::DEFINE_ABBREV:
      Stream.ReadAbbrevRecord();
      ++BlockStats.NumAbbrevs;
      break;
    default:
      ++BlockStats.NumRecords;
      if (AbbrevID != bitc::UNABBREV_RECORD)
        ++BlockStats.NumAbbreviatedRecords;
      
      Record.clear();
      unsigned Code = Stream.ReadRecord(AbbrevID, Record);

      // Increment the # occurrences of this code.
      if (BlockStats.CodeFreq.size() <= Code)
        BlockStats.CodeFreq.resize(Code+1);
      BlockStats.CodeFreq[Code]++;
      
      if (Dump) {
        std::cerr << Indent << "  <";
        if (const char *CodeName = GetCodeName(Code, BlockID))
          std::cerr << CodeName;
        else
          std::cerr << "UnknownCode" << Code;
        if (NonSymbolic && GetCodeName(Code, BlockID))
          std::cerr << " codeid=" << Code;
        if (AbbrevID != bitc::UNABBREV_RECORD)
          std::cerr << " abbrevid=" << AbbrevID;

        for (unsigned i = 0, e = Record.size(); i != e; ++i)
          std::cerr << " op" << i << "=" << (int64_t)Record[i];
        
        std::cerr << "/>\n";
      }
      
      break;
    }
  }
}
int main(int argc, char *argv[])
{
	BitstreamReader *br;
	BitstreamReader::STATUS sts;
	struct _stati64 st;
	unsigned __int64 file_size;
	const char *in_file;
	const char *out_file;
	int bit_count = 0;
	int bit_count_mod_8 = 0;
	unsigned __int64 byte_count = 0;
	srand(0);
	FILE *out_fp;
	int i;
	u32 bits;
	HANDLE h_in_file;
	HANDLE h_mem_mapping;
	unsigned char *mem_buf;



	if (argc != 3) {
		fprintf(stderr,"Usage: %s <in_file> <out_file>\n",argv[0]);
		exit(1);
	}

	in_file = argv[1];
	out_file = argv[2];

	if (_stati64(in_file,&st) < 0) {
		perror("Error: Cannot stat input file");
		exit(1);
	}
	
	file_size = st.st_size;
 
	printf("Size of file '%s' is: %I64u bytes\n",in_file,(__int64)file_size);
	if (file_size >= 0x10000000I64) {
		fprintf(stderr,"Error: File is larger than %I64 bytes\n",0x10000000I64);
		exit(1);
	}

	h_in_file = CreateFile(in_file,GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL);
	if (h_in_file == INVALID_HANDLE_VALUE) {
		fprintf(stderr,"Error: Cannot open input file '%s'\n",in_file);
		exit(1);
	}

	h_mem_mapping = CreateFileMapping(h_in_file,NULL,PAGE_READONLY,0,0,NULL);
	if (h_mem_mapping == NULL) {
		fprintf(stderr,"Error: Cannot create file mapping object for file: %s\n",in_file);
		CloseHandle(h_in_file);
		exit(1);
	}


	mem_buf = (unsigned char*)MapViewOfFile(h_mem_mapping,FILE_MAP_READ,0,0,(DWORD)file_size);
	if (mem_buf == NULL) {
		fprintf(stderr,"Error: Cannot map file '%s'\n",in_file);
		CloseHandle(h_mem_mapping);
		CloseHandle(h_in_file);
		exit(1);
	}

			
	sts = BitstreamReader::create(mem_buf,(size_t)file_size,&br);
	if (sts != BitstreamReader::STS_OK) {
		fprintf(stderr,"Error: Cannot create bitstream reader\n");
		CloseHandle(h_mem_mapping);
		CloseHandle(h_in_file);
		exit(1);
	}

	out_fp = fopen(out_file,"w");
	if (out_fp == NULL) {
		perror("Error: Cannot open output file");
		br->destroy();
		CloseHandle(h_mem_mapping);
		CloseHandle(h_in_file);
		exit(1);
	}


	while (file_size - byte_count >= 4) {
		int num_bits = (int)((double)rand()/RAND_MAX*32);

	//	printf("Attempting to read %d bytes\n",num_bits);

		sts = br->readBits(&bits,num_bits);

		if (sts != BitstreamReader::STS_OK) {
			fprintf(stderr,"Error reading %d bits from the input file\n",num_bits);
			fclose(out_fp);
			br->destroy();
			CloseHandle(h_mem_mapping);
			CloseHandle(h_in_file);
			exit(1);
		}

		for (i = num_bits-1; i>=0; i--) {
			if (bits & (u32(1) << i)) {
				putc('1',out_fp);
			} else {
				putc('0',out_fp);
			}
		}
	
//		bit_count += num_bits;
//		byte_count = bit_count/8;
		bit_count = (bit_count_mod_8 + num_bits);
		byte_count += (bit_count)/8;
		bit_count_mod_8 = bit_count % 8;
	}

	bit_count = (32 - (bit_count%32));
	sts = br->readBits(&bits,bit_count);
	
	if (sts != BitstreamReader::STS_OK) {
		fprintf(stderr,"Error reading %d bits from the input file\n",32-bit_count);
		fclose(out_fp);
		br->destroy();
		CloseHandle(h_mem_mapping);
		CloseHandle(h_in_file);
		exit(1);
	}

	for (i = bit_count -1; i>=0; i--) {

		if (bits & (u32(1) << i)) {
			putc('1',out_fp);
		} else {
			putc('0',out_fp);
		}
	}	

	fclose(out_fp);
	br->destroy();
	CloseHandle(h_mem_mapping);
	CloseHandle(h_in_file);

	return 0;
} 
Example #13
0
int
flacdec_read_metadata(BitstreamReader *bitstream,
                      struct flac_STREAMINFO *streaminfo,
                      a_obj *seektable,
                      int *channel_mask)
{
    BitstreamReader *comment = br_substream_new(BS_LITTLE_ENDIAN);

    enum {
        fL =  0x1,
        fR =  0x2,
        fC =  0x4,
        LFE = 0x8,
        bL =  0x10,
        bR =  0x20,
        bC =  0x100,
        sL =  0x200,
        sR =  0x400
    };

    if (!setjmp(*br_try(bitstream))) {
        unsigned last_block;

        if (bitstream->read(bitstream, 32) != 0x664C6143u) {
#ifndef STANDALONE
            PyErr_SetString(PyExc_ValueError, "not a FLAC file");
#endif
            br_etry(bitstream);
            comment->close(comment);
            return 1;
        }

        do {
            last_block = bitstream->read(bitstream, 1);
            const unsigned block_type = bitstream->read(bitstream, 7);
            const unsigned block_length = bitstream->read(bitstream, 24);

            switch (block_type) {
            case 0:   /*STREAMINFO*/
                streaminfo->minimum_block_size =
                    bitstream->read(bitstream, 16);
                streaminfo->maximum_block_size =
                    bitstream->read(bitstream, 16);
                streaminfo->minimum_frame_size =
                    bitstream->read(bitstream, 24);
                streaminfo->maximum_frame_size =
                    bitstream->read(bitstream, 24);
                streaminfo->sample_rate =
                    bitstream->read(bitstream, 20);
                streaminfo->channels =
                    bitstream->read(bitstream, 3) + 1;
                streaminfo->bits_per_sample =
                    bitstream->read(bitstream, 5) + 1;
                streaminfo->total_samples =
                    bitstream->read_64(bitstream, 36);

                bitstream->read_bytes(bitstream, streaminfo->md5sum, 16);

                /*default channel mask based on channel count*/
                switch (streaminfo->channels) {
                case 1:
                    *channel_mask = fC;
                    break;
                case 2:
                    *channel_mask = fL | fR;
                    break;
                case 3:
                    *channel_mask = fL | fR | fC;
                    break;
                case 4:
                    *channel_mask = fL | fR | bL | bR;
                    break;
                case 5:
                    *channel_mask = fL | fR | fC | bL | bR;
                    break;
                case 6:
                    *channel_mask = fL | fR | fC | LFE | bL | bR;
                    break;
                case 7:
                    *channel_mask = fL | fR | fC | LFE | bC | sL | sR;
                    break;
                case 8:
                    *channel_mask = fL | fR | fC | LFE | bL | bR | sL | sR;
                    break;
                default:
                    /*shouldn't be able to happen*/
                    *channel_mask = 0;
                    break;
                }
                break;
            case 3: /*SEEKTABLE*/
                {
                    unsigned seekpoints = block_length / 18;
                    seektable->reset_for(seektable, seekpoints);

                    for (; seekpoints > 0; seekpoints--) {
                        struct flac_SEEKPOINT seekpoint;
                        seekpoint.sample_number =
                            bitstream->read_64(bitstream, 64);
                        seekpoint.byte_offset =
                            bitstream->read_64(bitstream, 64);
                        seekpoint.samples =
                            bitstream->read(bitstream, 16);
                        seektable->append(seektable, &seekpoint);
                    }
                }
                break;
            case 4: /*VORBIS_COMMENT*/
                {
                    /*Vorbis comment's channel mask - if any -
                      overrides default one from channel count */

                    br_substream_reset(comment);
                    bitstream->substream_append(bitstream,
                                                comment,
                                                block_length);

                    flacdec_read_vorbis_comment(comment,
                                                streaminfo->channels,
                                                channel_mask);
                }
                break;
            default:  /*all other blocks*/
                bitstream->skip(bitstream, block_length * 8);
                break;
            }
        } while (!last_block);

        br_etry(bitstream);
        comment->close(comment);
        return 0;
    } else {
#ifndef STANDALONE
        PyErr_SetString(PyExc_IOError, "EOF while reading metadata");
#endif
        br_etry(bitstream);
        comment->close(comment);
        return 1;
    }
}
Example #14
0
HrdParameters HevcNalDecode::processHrdParameters(uint8_t commonInfPresentFlag, std::size_t maxNumSubLayersMinus1, BitstreamReader &bs)
{
  HrdParameters hrd;

  hrd.toDefault();

  hrd.nal_hrd_parameters_present_flag = 0;
  hrd.vcl_hrd_parameters_present_flag = 0;
  hrd.sub_pic_hrd_params_present_flag = 0;
  hrd.sub_pic_cpb_params_in_pic_timing_sei_flag = 0;
  if(commonInfPresentFlag)
  {
    hrd.nal_hrd_parameters_present_flag = bs.getBits(1);
    hrd.vcl_hrd_parameters_present_flag = bs.getBits(1);

    if(hrd.nal_hrd_parameters_present_flag || hrd.vcl_hrd_parameters_present_flag)
    {
      hrd.sub_pic_hrd_params_present_flag = bs.getBits(1);
      if(hrd.sub_pic_hrd_params_present_flag)
      {
        hrd.tick_divisor_minus2 = bs.getBits(8);
        hrd.du_cpb_removal_delay_increment_length_minus1 = bs.getBits(5);
        hrd.sub_pic_cpb_params_in_pic_timing_sei_flag = bs.getBits(1);
        hrd.dpb_output_delay_du_length_minus1 = bs.getBits(5);
      }
      hrd.bit_rate_scale = bs.getBits(4);
      hrd.cpb_size_scale = bs.getBits(4);

      if(hrd.sub_pic_hrd_params_present_flag)
        hrd.cpb_size_du_scale = bs.getBits(4);

      hrd.initial_cpb_removal_delay_length_minus1 = bs.getBits(5);
      hrd.au_cpb_removal_delay_length_minus1 = bs.getBits(5);
      hrd.dpb_output_delay_length_minus1 = bs.getBits(5);
    }
  }

  hrd.fixed_pic_rate_general_flag.resize(maxNumSubLayersMinus1 + 1);
  hrd.fixed_pic_rate_within_cvs_flag.resize(maxNumSubLayersMinus1 + 1);
  hrd.elemental_duration_in_tc_minus1.resize(maxNumSubLayersMinus1 + 1);
  hrd.low_delay_hrd_flag.resize(maxNumSubLayersMinus1 + 1, 0);
  hrd.cpb_cnt_minus1.resize(maxNumSubLayersMinus1 + 1, 0);

  if(hrd.nal_hrd_parameters_present_flag)
    hrd.nal_sub_layer_hrd_parameters.resize(maxNumSubLayersMinus1 + 1);
  if(hrd.vcl_hrd_parameters_present_flag)
    hrd.vcl_sub_layer_hrd_parameters.resize(maxNumSubLayersMinus1 + 1);

  for(std::size_t i = 0; i <= maxNumSubLayersMinus1; i++ )
  {
    hrd.fixed_pic_rate_general_flag[i] = bs.getBits(1);

    if(hrd.fixed_pic_rate_general_flag[i])
      hrd.fixed_pic_rate_within_cvs_flag[i] = 1;

    if(!hrd.fixed_pic_rate_general_flag[i])
       hrd.fixed_pic_rate_within_cvs_flag[i] = bs.getBits(1);

    if(hrd.fixed_pic_rate_within_cvs_flag[i])
      hrd.elemental_duration_in_tc_minus1[i] = bs.getGolombU();
    else
      hrd.low_delay_hrd_flag[i] = bs.getBits(1);

    if(!hrd.low_delay_hrd_flag[i])
      hrd.cpb_cnt_minus1[i] = bs.getGolombU();

    if(hrd.nal_hrd_parameters_present_flag)
      hrd.nal_sub_layer_hrd_parameters[i] = processSubLayerHrdParameters(hrd.sub_pic_hrd_params_present_flag, hrd.cpb_cnt_minus1[i], bs);
    if(hrd.vcl_hrd_parameters_present_flag)
      hrd.vcl_sub_layer_hrd_parameters[i] = processSubLayerHrdParameters(hrd.sub_pic_hrd_params_present_flag, hrd.cpb_cnt_minus1[i], bs);
  }

  return hrd;
}
Example #15
0
PyObject*
verifymodule_ogg(PyObject *dummy, PyObject *args) {
    PyObject *file_obj;
    BitstreamReader *bitstream;
    int has_previous_header = 0;
    struct ogg_header previous_header;
    struct ogg_header header;
    uint8_t *data_buffer = NULL;
    int data_buffer_size = 0;
    int i;
    uint32_t checksum;

    /*fixes a "may be used unitialized" warning*/
    previous_header.bitstream_serial_number =
        previous_header.page_sequence_number = 0;

    if (!PyArg_ParseTuple(args, "O", &file_obj))
        return NULL;

    if (!PyFile_CheckExact(file_obj)) {
        PyErr_SetString(PyExc_TypeError,
                        "first argument must be an actual file object");
        return NULL;
    } else {
        bitstream = br_open(PyFile_AsFile(file_obj), BS_LITTLE_ENDIAN);
        br_add_callback(bitstream, ogg_crc, &checksum);
    }

    if (!setjmp(*br_try(bitstream))) {
        do {
            checksum = 0;
            if (verifymodule_read_ogg_header(bitstream, &header) == OK) {
                if (data_buffer_size < header.segment_length_total) {
                    data_buffer = realloc(data_buffer,
                                          header.segment_length_total);
                    data_buffer_size = header.segment_length_total;
                }
                if (fread(data_buffer,
                          sizeof(uint8_t),
                          header.segment_length_total,
                          bitstream->input.file) !=
                    header.segment_length_total) {
                    PyErr_SetString(PyExc_IOError, "I/O error reading stream");
                    goto error;
                }

                for (i = 0; i < header.segment_length_total; i++)
                    ogg_crc(data_buffer[i], &checksum);
                if (header.checksum != checksum) {
                    PyErr_SetString(PyExc_ValueError,
                                    "checksum mismatch in stream");
                    goto error;
                }

                /* printf("calculated checksum : 0x%8.8X\n", checksum); */

                if (has_previous_header) {
                    if (header.bitstream_serial_number !=
                        previous_header.bitstream_serial_number) {
                        PyErr_SetString(PyExc_ValueError,
                                        "differing serial numbers in stream");
                        goto error;
                    }
                    if (header.page_sequence_number !=
                        (previous_header.page_sequence_number + 1)) {
                        PyErr_SetString(PyExc_ValueError,
                                        "page sequence number not incrementing");
                        goto error;
                    }
                    previous_header = header;
                } else {
                    previous_header = header;
                    has_previous_header = 1;
                }
            } else {
                goto error;
            }
        } while (!(header.type & 0x4));
    } else {
        PyErr_SetString(PyExc_IOError, "I/O error reading stream");
        goto error;
    }

    br_etry(bitstream);
    free(data_buffer);
    bitstream->input.file = NULL;
    bitstream->free(bitstream);
    Py_INCREF(Py_None);
    return Py_None;
 error:
    br_etry(bitstream);
    if (data_buffer != NULL)
        free(data_buffer);
    bitstream->input.file = NULL;
    bitstream->free(bitstream);
    return NULL;
}
int main(int argc, char *argv[])
{
    BitstreamReader *br;
    BitstreamReader::STATUS sts;
    struct _stati64 st;
    unsigned __int64 file_size;
    const char *in_file;
    const char *out_file;
    int bit_count = 0;
    int bit_count_mod_8 = 0;
    unsigned __int64 byte_count = 0;
    srand(0);
    FILE *out_fp;
    int i;
    u32 bits;


    if (argc != 3) {
        fprintf(stderr,"Usage: %s <in_file> <out_file>\n",argv[0]);
        exit(1);
    }

    in_file = argv[1];
    out_file = argv[2];

    if (_stati64(in_file,&st) < 0) {
        perror("Error: Cannot stat input file");
        exit(1);
    }

    file_size = st.st_size;

    printf("Size of file '%s' is: %I64u bytes\n",in_file,(__int64)file_size);

    sts = BitstreamReader::create(in_file,&br);
    if (sts != BitstreamReader::STS_OK) {
        fprintf(stderr,"Error: Cannot create bitstream reader\n");
        exit(1);
    }

    out_fp = fopen(out_file,"w");
    if (out_fp == NULL) {
        perror("Error: Cannot open output file");
        br->destroy();
        exit(1);
    }


    while (file_size - byte_count >= 4) {
        int num_bits = (int)((double)rand()/RAND_MAX*32);

        //	printf("Attempting to read %d bytes\n",num_bits);

        sts = br->readBits(&bits,num_bits);

        if (sts != BitstreamReader::STS_OK) {
            fprintf(stderr,"Error reading %d bits from the input file\n",num_bits);
            br->destroy();
            exit(1);
        }

        for (i = num_bits-1; i>=0; i--) {
            if (bits & (u32(1) << i)) {
                putc('1',out_fp);
            } else {
                putc('0',out_fp);
            }
        }

//		bit_count += num_bits;
//		byte_count = bit_count/8;
        bit_count = (bit_count_mod_8 + num_bits);
        byte_count += (bit_count)/8;
        bit_count_mod_8 = bit_count % 8;
    }

    bit_count = (32 - (bit_count%32));
    sts = br->readBits(&bits,bit_count);

    if (sts != BitstreamReader::STS_OK) {
        fprintf(stderr,"Error reading %d bits from the input file\n",32-bit_count);
        br->destroy();
        exit(1);
    }

    for (i = bit_count -1; i>=0; i--) {

        if (bits & (u32(1) << i)) {
            putc('1',out_fp);
        } else {
            putc('0',out_fp);
        }
    }

    fclose(out_fp);

    br->destroy();

    return 0;
}
Example #17
0
ShortTermRefPicSet HevcNalDecode::processShortTermRefPicSet(std::size_t stRpsIdx, std::size_t num_short_term_ref_pic_sets, const std::vector<ShortTermRefPicSet> &refPicSets, std::shared_ptr<SPS> psps, BitstreamReader &bs)
{
  ShortTermRefPicSet rpset;

  rpset.toDefault();

  rpset.inter_ref_pic_set_prediction_flag = 0;
  rpset.delta_idx_minus1 = 0;
  if(stRpsIdx)
  {
    rpset.inter_ref_pic_set_prediction_flag = bs.getBits(1);
  }

  if(rpset.inter_ref_pic_set_prediction_flag)
  {
    if(stRpsIdx == num_short_term_ref_pic_sets)
      rpset.delta_idx_minus1 = bs.getGolombU();

    rpset.delta_rps_sign = bs.getBits(1);
    rpset.abs_delta_rps_minus1 = bs.getGolombU();

    std::size_t RefRpsIdx = stRpsIdx - (rpset.delta_idx_minus1 + 1);
    std::size_t NumDeltaPocs = 0;

    if(refPicSets[RefRpsIdx].inter_ref_pic_set_prediction_flag)
    {
      for(std::size_t i=0; i<refPicSets[RefRpsIdx].used_by_curr_pic_flag.size(); i++)
        if(refPicSets[RefRpsIdx].used_by_curr_pic_flag[i] || refPicSets[RefRpsIdx].use_delta_flag[i])
          NumDeltaPocs++;
    }
    else
      NumDeltaPocs = refPicSets[RefRpsIdx].num_negative_pics + refPicSets[RefRpsIdx].num_positive_pics;

    rpset.used_by_curr_pic_flag.resize(NumDeltaPocs + 1);
    rpset.use_delta_flag.resize(NumDeltaPocs + 1, 1);

    for(std::size_t i=0; i<=NumDeltaPocs; i++ )
    {
      rpset.used_by_curr_pic_flag[i] = bs.getBits(1);
      if(!rpset.used_by_curr_pic_flag[i])
        rpset.use_delta_flag[i] = bs.getBits(1);
    }
  }
  else
  {
    rpset.num_negative_pics = bs.getGolombU();
    rpset.num_positive_pics = bs.getGolombU();

    if(rpset.num_negative_pics > psps -> sps_max_dec_pic_buffering_minus1[psps -> sps_max_sub_layers_minus1])
    {
      LogDebug("HevcNalDecode:ShortTermRefPicSet: num_negative_pics > sps_max_dec_pic_buffering_minus1");
      return rpset;
    }

    if(rpset.num_positive_pics > psps -> sps_max_dec_pic_buffering_minus1[psps -> sps_max_sub_layers_minus1])
    {
      LogDebug("HevcNalDecode:ShortTermRefPicSet: num_positive_pics > sps_max_dec_pic_buffering_minus1");
      return rpset;      
    }

    rpset.delta_poc_s0_minus1.resize(rpset.num_negative_pics);
    rpset.used_by_curr_pic_s0_flag.resize(rpset.num_negative_pics);

    for(std::size_t i=0; i<rpset.num_negative_pics; i++)
    {
      rpset.delta_poc_s0_minus1[i] = bs.getGolombU();
      rpset.used_by_curr_pic_s0_flag[i] = bs.getBits(1);
    }

    rpset.delta_poc_s1_minus1.resize(rpset.num_positive_pics);
    rpset.used_by_curr_pic_s1_flag.resize(rpset.num_positive_pics);
    for(std::size_t i=0; i<rpset.num_positive_pics; i++)
    {
      rpset.delta_poc_s1_minus1[i] = bs.getGolombU();
      rpset.used_by_curr_pic_s1_flag[i] = bs.getBits(1);
    }

  }

  return rpset;
}
Example #18
0
VuiParameters HevcNalDecode::processVuiParameters(std::size_t sps_max_sub_layers_minus1, BitstreamReader &bs)
{
  VuiParameters vui;

  vui.toDefault();

  vui.aspect_ratio_idc = 0;
  vui.sar_width = 0;
  vui.sar_height = 0;


  vui.aspect_ratio_info_present_flag = bs.getBits(1);

  if(vui.aspect_ratio_info_present_flag)
  {
    vui.aspect_ratio_idc = bs.getBits(8);

    if(vui.aspect_ratio_idc == 255) //EXTENDED_SAR
    {
      vui.sar_width = bs.getBits(16);
      vui.sar_height = bs.getBits(16);
    }
  }


  vui.overscan_info_present_flag = bs.getBits(1);
  if(vui.overscan_info_present_flag)
    vui.overscan_appropriate_flag = bs.getBits(1);

  vui.video_format = 5;
  vui.video_full_range_flag = 0;
  vui.colour_primaries = 2;
  vui.transfer_characteristics = 2;
  vui.matrix_coeffs = 2;

  vui.video_signal_type_present_flag = bs.getBits(1);

  if(vui.video_signal_type_present_flag)
  {
    vui.video_format = bs.getBits(3);
    vui.video_full_range_flag = bs.getBits(1);
    vui.colour_description_present_flag = bs.getBits(1);

    if(vui.colour_description_present_flag)
    {
      vui.colour_primaries = bs.getBits(8);
      vui.transfer_characteristics = bs.getBits(8);
      vui.matrix_coeffs = bs.getBits(8);
    }

  }

  vui.chroma_sample_loc_type_top_field = 0;
  vui.chroma_sample_loc_type_bottom_field = 0;

  vui.chroma_loc_info_present_flag = bs.getBits(1);
  if(vui.chroma_loc_info_present_flag)
  {
    vui.chroma_sample_loc_type_top_field = bs.getGolombU();
    vui.chroma_sample_loc_type_bottom_field = bs.getGolombU();
  }


  vui.neutral_chroma_indication_flag = bs.getBits(1);
  vui.field_seq_flag = bs.getBits(1);
  vui.frame_field_info_present_flag = bs.getBits(1);
  vui.default_display_window_flag = bs.getBits(1);

  vui.def_disp_win_left_offset = 0;
  vui.def_disp_win_right_offset = 0;
  vui.def_disp_win_right_offset = 0;
  vui.def_disp_win_bottom_offset = 0;

  if(vui.default_display_window_flag)
  {
    vui.def_disp_win_left_offset = bs.getGolombU();
    vui.def_disp_win_right_offset = bs.getGolombU();
    vui.def_disp_win_top_offset = bs.getGolombU();
    vui.def_disp_win_bottom_offset = bs.getGolombU();
  }

  vui.vui_timing_info_present_flag = bs.getBits(1);

  if(vui.vui_timing_info_present_flag)
  {
    vui.vui_num_units_in_tick = bs.getBits(32);
    vui.vui_time_scale = bs.getBits(32);
    vui.vui_poc_proportional_to_timing_flag = bs.getBits(1);

    if(vui.vui_poc_proportional_to_timing_flag)
      vui.vui_num_ticks_poc_diff_one_minus1 = bs.getGolombU();

    vui.vui_hrd_parameters_present_flag = bs.getBits(1);

    if(vui.vui_hrd_parameters_present_flag)
      vui.hrd_parameters = processHrdParameters(1, sps_max_sub_layers_minus1, bs);
  }

  vui.bitstream_restriction_flag = bs.getBits(1);

  if(vui.bitstream_restriction_flag)
  {
    vui.tiles_fixed_structure_flag = bs.getBits(1);
    vui.motion_vectors_over_pic_boundaries_flag = bs.getBits(1);
    vui.restricted_ref_pic_lists_flag = bs.getBits(1);

    vui.min_spatial_segmentation_idc = bs.getGolombU();
    vui.max_bytes_per_pic_denom = bs.getGolombU();
    vui.max_bits_per_min_cu_denom = bs.getGolombU();
    vui.log2_max_mv_length_horizontal = bs.getGolombU();
    vui.log2_max_mv_length_vertical = bs.getGolombU();
  }

  return vui;
}
Example #19
0
ProfileTierLevel HevcNalDecode::processProfileTierLevel(std::size_t max_sub_layers_minus1, BitstreamReader &bs)
{
  ProfileTierLevel ptl;

  ptl.toDefault();

  ptl.general_profile_space = bs.getBits(2);
  ptl.general_tier_flag = bs.getBits(1);
  ptl.general_profile_idc = bs.getBits(5);

  for(std::size_t i=0; i<32; i++)
    ptl.general_profile_compatibility_flag[i] = bs.getBits(1);

  ptl.general_progressive_source_flag = bs.getBits(1);
  ptl.general_interlaced_source_flag = bs.getBits(1);
  ptl.general_non_packed_constraint_flag = bs.getBits(1);
  ptl.general_frame_only_constraint_flag = bs.getBits(1);
  bs.getBits(32);
  bs.getBits(12);
  ptl.general_level_idc = bs.getBits(8);

  ptl.sub_layer_profile_present_flag.resize(max_sub_layers_minus1);
  ptl.sub_layer_level_present_flag.resize(max_sub_layers_minus1);

  for(std::size_t i=0; i<max_sub_layers_minus1; i++)
  {
    ptl.sub_layer_profile_present_flag[i] = bs.getBits(1);
    ptl.sub_layer_level_present_flag[i] = bs.getBits(1);
  }


  if(max_sub_layers_minus1 > 0)
  {
    for(std::size_t i=max_sub_layers_minus1; i<8; i++)
      bs.getBits(2);
  }

  ptl.sub_layer_profile_space.resize(max_sub_layers_minus1);
  ptl.sub_layer_tier_flag.resize(max_sub_layers_minus1);
  ptl.sub_layer_profile_idc.resize(max_sub_layers_minus1);
  ptl.sub_layer_profile_compatibility_flag.resize(max_sub_layers_minus1);
  ptl.sub_layer_progressive_source_flag.resize(max_sub_layers_minus1);
  ptl.sub_layer_interlaced_source_flag.resize(max_sub_layers_minus1);
  ptl.sub_layer_non_packed_constraint_flag.resize(max_sub_layers_minus1);
  ptl.sub_layer_frame_only_constraint_flag.resize(max_sub_layers_minus1);
  ptl.sub_layer_level_idc.resize(max_sub_layers_minus1);

  for(std::size_t i=0; i<max_sub_layers_minus1; i++)
  {
    if(ptl.sub_layer_profile_present_flag[i])
    {
      ptl.sub_layer_profile_space[i] = bs.getBits(2);
      ptl.sub_layer_tier_flag[i] = bs.getBits(1);
      ptl.sub_layer_profile_idc[i] = bs.getBits(5);
      ptl.sub_layer_profile_compatibility_flag[i].resize(32);

      for(std::size_t j=0; j<32; j++)
        ptl.sub_layer_profile_compatibility_flag[i][j] = bs.getBits(1);

      ptl.sub_layer_progressive_source_flag[i] = bs.getBits(1);
      ptl.sub_layer_interlaced_source_flag[i] = bs.getBits(1);
      ptl.sub_layer_non_packed_constraint_flag[i] = bs.getBits(1);
      ptl.sub_layer_frame_only_constraint_flag[i] = bs.getBits(1);
      bs.getBits(32);
      bs.getBits(12);

    }

    if(ptl.sub_layer_level_present_flag[i])
    {
      ptl.sub_layer_level_idc[i] = bs.getBits(8);
    }
    else
      ptl.sub_layer_level_idc[i] = 1;

  }

  return ptl;
}
Example #20
0
void HevcNalDecode::processSPS(std::shared_ptr<SPS> psps, BitstreamReader &bs)
{
  psps -> sps_video_parameter_set_id = bs.getBits(4);
  psps -> sps_max_sub_layers_minus1 = bs.getBits(3);
  psps -> sps_temporal_id_nesting_flag = bs.getBits(1);
  psps -> profile_tier_level = processProfileTierLevel(psps -> sps_max_sub_layers_minus1, bs);

  psps -> sps_seq_parameter_set_id = bs.getGolombU();
  psps -> chroma_format_idc = bs.getGolombU();

  if(psps -> chroma_format_idc == 3)
    psps -> separate_colour_plane_flag = bs.getBits(1);
  else
    psps -> separate_colour_plane_flag = 0;

  psps -> pic_width_in_luma_samples = bs.getGolombU();
  psps -> pic_height_in_luma_samples = bs.getGolombU();
  psps -> conformance_window_flag = bs.getBits(1);

  if(psps -> conformance_window_flag)
  {
    psps -> conf_win_left_offset = bs.getGolombU();
    psps -> conf_win_right_offset = bs.getGolombU();
    psps -> conf_win_top_offset = bs.getGolombU();
    psps -> conf_win_bottom_offset = bs.getGolombU();
  }

  psps -> bit_depth_luma_minus8 = bs.getGolombU();
  psps -> bit_depth_chroma_minus8 = bs.getGolombU();
  psps -> log2_max_pic_order_cnt_lsb_minus4 = bs.getGolombU();
  psps -> sps_sub_layer_ordering_info_present_flag = bs.getBits(1);

  psps -> sps_max_dec_pic_buffering_minus1.resize(psps -> sps_max_sub_layers_minus1 + 1, 0);
  psps -> sps_max_num_reorder_pics.resize(psps -> sps_max_sub_layers_minus1 + 1, 0);
  psps -> sps_max_latency_increase_plus1.resize(psps -> sps_max_sub_layers_minus1 + 1, 0);

  for(std::size_t i=(psps -> sps_sub_layer_ordering_info_present_flag ? 0 : psps -> sps_max_sub_layers_minus1);
      i<=psps -> sps_max_sub_layers_minus1;
      i++)
  {
    psps -> sps_max_dec_pic_buffering_minus1[i] = bs.getGolombU();
    psps -> sps_max_num_reorder_pics[i] = bs.getGolombU();
    psps -> sps_max_latency_increase_plus1[i] = bs.getGolombU();
  }

  psps -> log2_min_luma_coding_block_size_minus3 = bs.getGolombU();
  psps -> log2_diff_max_min_luma_coding_block_size = bs.getGolombU();
  psps -> log2_min_transform_block_size_minus2 = bs.getGolombU();
  psps -> log2_diff_max_min_transform_block_size = bs.getGolombU();
  psps -> max_transform_hierarchy_depth_inter = bs.getGolombU();
  psps -> max_transform_hierarchy_depth_intra = bs.getGolombU();

  psps -> scaling_list_enabled_flag = bs.getBits(1);
  if(psps -> scaling_list_enabled_flag)
  {
    psps -> sps_scaling_list_data_present_flag = bs.getBits(1);
    if(psps -> sps_scaling_list_data_present_flag)
    {
      psps -> scaling_list_data = processScalingListData(bs);
    }
  }

  psps -> amp_enabled_flag = bs.getBits(1);
  psps -> sample_adaptive_offset_enabled_flag = bs.getBits(1);
  psps -> pcm_enabled_flag = bs.getBits(1);

  if(psps -> pcm_enabled_flag)
  {
    psps -> pcm_sample_bit_depth_luma_minus1 = bs.getBits(4);
    psps -> pcm_sample_bit_depth_chroma_minus1 = bs.getBits(4);
    psps -> log2_min_pcm_luma_coding_block_size_minus3 = bs.getGolombU();
    psps -> log2_diff_max_min_pcm_luma_coding_block_size = bs.getGolombU();
    psps -> pcm_loop_filter_disabled_flag = bs.getBits(1);
  }

  psps -> num_short_term_ref_pic_sets = bs.getGolombU();

  psps -> short_term_ref_pic_set.resize(psps -> num_short_term_ref_pic_sets);
  for(std::size_t i=0; i<psps -> num_short_term_ref_pic_sets; i++)
    psps -> short_term_ref_pic_set[i] = processShortTermRefPicSet(i, psps -> num_short_term_ref_pic_sets, psps -> short_term_ref_pic_set, psps, bs);

  psps -> long_term_ref_pics_present_flag = bs.getBits(1);
  if(psps -> long_term_ref_pics_present_flag)
  {
    psps -> num_long_term_ref_pics_sps = bs.getGolombU();
    psps -> lt_ref_pic_poc_lsb_sps.resize(psps -> num_long_term_ref_pics_sps);
    psps -> used_by_curr_pic_lt_sps_flag.resize(psps -> num_long_term_ref_pics_sps);

    for(std::size_t i = 0; i<psps -> num_long_term_ref_pics_sps; i++)
    {
      psps -> lt_ref_pic_poc_lsb_sps[i] = bs.getBits(psps -> log2_max_pic_order_cnt_lsb_minus4 + 4);
      psps -> used_by_curr_pic_lt_sps_flag[i] = bs.getBits(1);
    }
  }

  psps -> sps_temporal_mvp_enabled_flag = bs.getBits(1);
  psps -> strong_intra_smoothing_enabled_flag = bs.getBits(1);
  psps -> vui_parameters_present_flag = bs.getBits(1);

  if(psps -> vui_parameters_present_flag)
  {
    psps -> vui_parameters = processVuiParameters(psps -> sps_max_sub_layers_minus1, bs);
  }

  psps -> sps_extension_flag = bs.getBits(1);
}
static Status parseAPP0(DataReader *pReader, JPEGAPP0Header *pHeader)
{
    BitstreamReader bs;

    Ipp8u  iMarker[5];
    size_t iSize = 5;

    pReader->Get16uSwap(&pHeader->iLength);

    pReader->CacheData(&iMarker[0], iSize);
    if(iMarker[0] == 'J' && iMarker[1] == 'F' && iMarker[2] == 'I' && iMarker[3] == 'F' && iMarker[4] == 0)
    {
        pReader->MovePosition(5);
        bs.Init(pReader);

        pHeader->bJfifDetected    = true;
        pHeader->iJfifVerMajor    = bs.GetBits(8);
        pHeader->iJfifVerMinor    = bs.GetBits(8);
        pHeader->iJfifUnits       = bs.GetBits(8);
        pHeader->iJfifXDensity    = bs.GetBits(16);
        pHeader->iJfifYDensity    = bs.GetBits(16);
        pHeader->iJfifThumbWidth  = bs.GetBits(8);
        pHeader->iJfifThumbHeight = bs.GetBits(8);

        bs.AlignReader();
    }

    pReader->CacheData(&iMarker[0], iSize);
    if(iMarker[0] == 'J' && iMarker[1] == 'F' && iMarker[2] == 'X' && iMarker[3] == 'X' && iMarker[4] == 0)
    {
        pReader->MovePosition(5);
        bs.Init(pReader);

        pHeader->bJfxxDetected       = true;
        pHeader->iJfxxThumbnailsType = bs.GetBits(8);

        bs.AlignReader();
    }

    pReader->CacheData(&iMarker[0], iSize);
    if(iMarker[0] == 'A' && iMarker[1] == 'V' && iMarker[2] == 'I' && iMarker[3] == '1')
    {
        pReader->MovePosition(4);
        bs.Init(pReader);

        pHeader->bAvi1Detected   = true;
        pHeader->iAvi1Polarity   = bs.GetBits(8);
        pHeader->iAvi1Reserved   = bs.GetBits(8);
        pHeader->iAvi1FiledSize1 = bs.GetBits(32);
        pHeader->iAvi1FiledSize2 = bs.GetBits(32);

        bs.AlignReader();
    }

    return UMC_OK;
}
Example #22
0
int main(int argc, char* argv[]) {
    FILE* ogg_file;
    OggReader* ogg_stream = NULL;
    BitstreamReader* packet = NULL;
    struct flac_STREAMINFO streaminfo;
    uint16_t header_packets;
    a_int* residuals = NULL;
    a_int* qlp_coeffs = NULL;
    aa_int* subframe_data = NULL;
    a_int* framelist_data = NULL;
    ogg_status result;
    uint16_t crc16 = 0;

    FrameList_int_to_char_converter converter;
    unsigned pcm_size;
    unsigned output_data_size = 1;
    uint8_t* output_data = NULL;

    audiotools__MD5Context md5;
    unsigned char stream_md5sum[16];
    const static unsigned char blank_md5sum[16] = {0, 0, 0, 0, 0, 0, 0, 0,
                                                   0, 0, 0, 0, 0, 0, 0, 0};

    if (argc < 2) {
        fprintf(stderr, "*** Usage: %s <file.oga>\n", argv[0]);
        return 1;
    }

    /*open input file for reading*/
    if ((ogg_file = fopen(argv[1], "rb")) == NULL) {
        fprintf(stderr, "*** %s: %s\n", argv[1], strerror(errno));
        return 1;
    } else {
        /*open bitstream and setup temporary arrays/buffers*/
        ogg_stream = oggreader_open(ogg_file);
        packet = br_substream_new(BS_BIG_ENDIAN);
        subframe_data = aa_int_new();
        residuals = a_int_new();
        qlp_coeffs = a_int_new();
        framelist_data = a_int_new();
        output_data = malloc(output_data_size);
    }

    /*the first packet should be the FLAC's STREAMINFO*/
    if ((result = oggreader_next_packet(ogg_stream, packet)) == OGG_OK) {
        if (!oggflac_read_streaminfo(packet, &streaminfo, &header_packets)) {
            goto error;
        } else {
            converter = FrameList_get_int_to_char_converter(
                streaminfo.bits_per_sample, 0, 1);
        }
    } else {
        fprintf(stderr, "*** Error: %s\n", ogg_strerror(result));
        goto error;
    }

    /*skip subsequent header packets*/
    for (; header_packets > 0; header_packets--) {
        if ((result = oggreader_next_packet(ogg_stream, packet)) != OGG_OK) {
            fprintf(stderr, "*** Error: %s\n", ogg_strerror(result));
            goto error;
        }
    }

    /*initialize the output MD5 sum*/
    audiotools__MD5Init(&md5);

    /*add callback for CRC16 calculation*/
    br_add_callback(packet, (bs_callback_f)flac_crc16, &crc16);

    /*decode the next FrameList from the stream*/
    result = oggreader_next_packet(ogg_stream, packet);

    while (result != OGG_STREAM_FINISHED) {
        if (result == OGG_OK) {
            flac_status flac_status;
            struct flac_frame_header frame_header;
            unsigned channel;

            subframe_data->reset(subframe_data);

            if (!setjmp(*br_try(packet))) {
                /*read frame header*/
                if ((flac_status =
                     flacdec_read_frame_header(packet,
                                               &streaminfo,
                                               &frame_header)) != OK) {
                    fprintf(stderr, "*** Error: %s\n",
                            FlacDecoder_strerror(flac_status));
                    br_etry(packet);
                    goto error;
                }

                /*read 1 subframe per channel*/
                for (channel = 0;
                     channel < frame_header.channel_count;
                     channel++)
                    if ((flac_status = flacdec_read_subframe(
                        packet,
                        qlp_coeffs,
                        residuals,
                        frame_header.block_size,
                        flacdec_subframe_bits_per_sample(&frame_header,
                                                         channel),
                        subframe_data->append(subframe_data))) != OK) {
                        fprintf(stderr, "*** Error: %s\n",
                                FlacDecoder_strerror(flac_status));
                        br_etry(packet);
                        goto error;
                    }

                br_etry(packet);
            } else {
                br_etry(packet);
                fprintf(stderr, "*** I/O Error reading FLAC frame\n");
                goto error;
            }

            /*handle difference channels, if any*/
            flacdec_decorrelate_channels(frame_header.channel_assignment,
                                         subframe_data,
                                         framelist_data);

            /*check CRC-16*/
            packet->byte_align(packet);
            packet->read(packet, 16);
            if (crc16 != 0) {
                fprintf(stderr, "*** Error: invalid checksum in frame\n");
                goto error;
            }

            /*turn FrameList into string of output*/
            pcm_size = (streaminfo.bits_per_sample / 8) * framelist_data->len;
            if (pcm_size > output_data_size) {
                output_data_size = pcm_size;
                output_data = realloc(output_data, output_data_size);
            }
            FrameList_samples_to_char(output_data,
                                      framelist_data->_,
                                      converter,
                                      framelist_data->len,
                                      streaminfo.bits_per_sample);

            /*update MD5 sum*/
            audiotools__MD5Update(&md5, output_data, pcm_size);

            /*output string to stdout*/
            fwrite(output_data, sizeof(unsigned char), pcm_size, stdout);

            result = oggreader_next_packet(ogg_stream, packet);
        } else {
            /*some error reading Ogg stream*/
            fprintf(stderr, "*** Error: %s\n", ogg_strerror(result));
            goto error;
        }
    }

    /*Ogg stream is finished so verify stream's MD5 sum*/
    audiotools__MD5Final(stream_md5sum, &md5);

    if (!((memcmp(streaminfo.md5sum, blank_md5sum, 16) == 0) ||
          (memcmp(stream_md5sum, streaminfo.md5sum, 16) == 0))) {
        fprintf(stderr, "*** MD5 mismatch at end of stream\n");
        goto error;
    }

    /*close streams, temporary buffers*/
    oggreader_close(ogg_stream);
    packet->close(packet);
    subframe_data->del(subframe_data);
    residuals->del(residuals);
    qlp_coeffs->del(qlp_coeffs);
    framelist_data->del(framelist_data);
    free(output_data);

    return 0;

error:
    oggreader_close(ogg_stream);
    packet->close(packet);
    subframe_data->del(subframe_data);
    residuals->del(residuals);
    qlp_coeffs->del(qlp_coeffs);
    framelist_data->del(framelist_data);
    free(output_data);

    return 1;
}
Example #23
0
static PyObject*
DVDA_Title_next_track(decoders_DVDA_Title *self, PyObject *args)
{
    unsigned PTS_ticks;
    DVDA_Packet next_packet;
    struct bs_buffer* packet_data = self->packet_data;
    unsigned i;

    if (!PyArg_ParseTuple(args, "I", &PTS_ticks))
        return NULL;

    /*ensure previous track has been exhausted, if any*/
    if (self->pcm_frames_remaining) {
        PyErr_SetString(PyExc_ValueError,
                        "current track has not been exhausted");
        return NULL;
    }

    /*read the next packet*/
    if (read_audio_packet(self->packet_reader,
                          &next_packet, packet_data)) {
        PyErr_SetString(PyExc_IOError,
                        "I/O error reading initialization packet");
        return NULL;
    }
#else
int
DVDA_Title_next_track(decoders_DVDA_Title *self, unsigned PTS_ticks)
{
    DVDA_Packet next_packet;
    struct bs_buffer* packet_data = self->packet_data;
    unsigned i;

    if (self->pcm_frames_remaining) {
        fprintf(stderr, "current track has not been exhausted\n");
        return 0;
    }

    if (read_audio_packet(self->packet_reader,
                          &next_packet, packet_data)) {
        fprintf(stderr, "I/O error reading initialization packet\n");
        return 0;
    }
#endif

    if (next_packet.codec_ID == PCM_CODEC_ID) {
        /*if the packet is PCM, initialize Title's stream attributes
          (bits-per-sample, sample rate, channel assignment/mask)
          with values taken from the first packet*/

        /*PCM stores stream attributes in the second padding block*/
        self->bits_per_sample =
            dvda_bits_per_sample(next_packet.PCM.group_1_bps);
        self->sample_rate =
            dvda_sample_rate(next_packet.PCM.group_1_rate);
        self->channel_assignment =
            next_packet.PCM.channel_assignment;
        self->channel_count =
            dvda_channel_count(next_packet.PCM.channel_assignment);
        self->channel_mask =
            dvda_channel_mask(next_packet.PCM.channel_assignment);

        self->frame_codec = PCM;

        init_aobpcm_decoder(&(self->pcm_decoder),
                            self->bits_per_sample,
                            self->channel_count);

        buf_extend(packet_data, self->frames);

    } else if (next_packet.codec_ID == MLP_CODEC_ID) {
        /*if the packet is MLP,
          check if the first frame starts with a major sync*/
        enum {PACKET_DATA};
        BitstreamReader* r = br_open_buffer(packet_data, BS_BIG_ENDIAN);
        r->mark(r, PACKET_DATA);
        if (!setjmp(*br_try(r))) {
            unsigned sync_words;
            unsigned stream_type;

            r->parse(r, "32p 24u 8u", &sync_words, &stream_type);
            if ((sync_words == 0xF8726F) && (stream_type == 0xBB)) {
                /*if so, discard any unconsumed packet data
                  and initialize Title's stream attributes
                  with values taken from the major sync*/

                unsigned group_1_bps;
                unsigned group_2_bps;
                unsigned group_1_rate;
                unsigned group_2_rate;

                r->parse(r, "4u 4u 4u 4u 11p 5u 48p",
                         &group_1_bps, &group_2_bps,
                         &group_1_rate, &group_2_rate,
                         &(self->channel_assignment));

                self->bits_per_sample =
                    dvda_bits_per_sample(group_1_bps);
                self->sample_rate =
                    dvda_sample_rate(group_1_rate);
                self->channel_count =
                    dvda_channel_count(self->channel_assignment);
                self->channel_mask =
                    dvda_channel_mask(self->channel_assignment);
                self->frame_codec = MLP;
                self->mlp_decoder->major_sync_read = 0;

                r->rewind(r, PACKET_DATA);
                r->unmark(r, PACKET_DATA);
                br_etry(r);
                r->close(r);

                buf_reset(self->frames);
                buf_extend(packet_data, self->frames);
            } else {
                /*if not, append packet data to any unconsumed data
                  and leave Title's stream attributes as they were*/

                r->rewind(r, PACKET_DATA);
                r->unmark(r, PACKET_DATA);
                br_etry(r);
                r->close(r);

                buf_extend(packet_data, self->frames);
            }
        } else {
            /*if I/O error reading major sync,
              append packet data to any unconsumed data
              and leave Title's stream attributes as they were*/

            r->rewind(r, PACKET_DATA);
            r->unmark(r, PACKET_DATA);
            br_etry(r);
            r->close(r);

            buf_extend(packet_data, self->frames);
        }

    } else {
#ifndef STANDALONE
        PyErr_SetString(PyExc_ValueError, "unknown codec ID");
        return NULL;
#else
        return 0;
#endif
    }

    /*convert PTS ticks to PCM frames based on sample rate*/
    self->pcm_frames_remaining = (unsigned)round((double)PTS_ticks *
                                                 (double)self->sample_rate /
                                                 (double)PTS_PER_SECOND);

    /*initalize codec's framelist with the proper number of channels*/
    if (self->codec_framelist->len != self->channel_count) {
        self->codec_framelist->reset(self->codec_framelist);
        for (i = 0; i < self->channel_count; i++)
            self->codec_framelist->append(self->codec_framelist);
    }

#ifndef STANDALONE
    Py_INCREF(Py_None);
    return Py_None;
#else
    return 1;
#endif
}
Example #24
0
static int
read_audio_packet(DVDA_Packet_Reader* packets,
                  DVDA_Packet* packet, struct bs_buffer* packet_data)
{
    if (packets->total_sectors) {
        BitstreamReader* reader = packets->reader;
        struct bs_buffer* buffer = reader->input.substream;
        buf_reset(buffer);
        if (!read_sector(packets->sectors, buffer)) {
            if (buf_window_size(buffer) == 0) {
                return 0;
            }

            if (!setjmp(*br_try(reader))) {
                unsigned sync_bytes;
                unsigned pad[6];
                unsigned PTS_high;
                unsigned PTS_mid;
                unsigned PTS_low;
                unsigned SCR_extension;
                unsigned bitrate;
                unsigned stuffing_count;
                int audio_packet_found = 0;

                /*read pack header*/
                reader->parse(reader,
                              "32u 2u 3u 1u 15u 1u 15u 1u 9u 1u 22u 2u 5p 3u",
                              &sync_bytes, &(pad[0]), &PTS_high,
                              &(pad[1]), &PTS_mid, &(pad[2]),
                              &PTS_low, &(pad[3]), &SCR_extension,
                              &(pad[4]), &bitrate, &(pad[5]),
                              &stuffing_count);
                if (sync_bytes != 0x000001BA) {
                    br_etry(reader);
                    return 1;
                }

                if ((pad[0] != 1) || (pad[1] != 1) || (pad[2] != 1) ||
                    (pad[3] != 1) || (pad[4] != 1) || (pad[5] != 3)) {
                    br_etry(reader);
                    return 1;
                }

                for (; stuffing_count; stuffing_count--) {
                    reader->skip(reader, 8);
                }

                /*read packets from sector until sector is empty*/
                while (buf_window_size(buffer) > 0) {
                    unsigned start_code;
                    unsigned stream_id;
                    unsigned packet_length;

                    reader->parse(reader, "24u 8u 16u",
                                  &start_code, &stream_id, &packet_length);

                    if (start_code != 0x000001) {
                        br_etry(reader);
                        return 1;
                    }

                    if (stream_id == 0xBD) {
                        /*audio packets are forwarded to packet*/
                        unsigned pad1_size;
                        unsigned pad2_size;

                        reader->parse(reader, "16p 8u", &pad1_size);
                        reader->skip_bytes(reader, pad1_size);
                        reader->parse(reader, "8u 8u 8p 8u",
                                      &(packet->codec_ID),
                                      &(packet->CRC),
                                      &pad2_size);

                        if (packet->codec_ID == 0xA0) { /*PCM*/
                            reader->parse(reader,
                                          "16u 8p 4u 4u 4u 4u 8p 8u 8p 8u",
                                          &(packet->PCM.first_audio_frame),
                                          &(packet->PCM.group_1_bps),
                                          &(packet->PCM.group_2_bps),
                                          &(packet->PCM.group_1_rate),
                                          &(packet->PCM.group_2_rate),
                                          &(packet->PCM.channel_assignment),
                                          &(packet->PCM.CRC));
                            reader->skip_bytes(reader, pad2_size - 9);
                        } else {                        /*probably MLP*/
                            reader->skip_bytes(reader, pad2_size);
                        }

                        packet_length -= 3 + pad1_size + 4 + pad2_size;

                        buf_reset(packet_data);
                        while (packet_length) {
                            static uint8_t buffer[4096];
                            const unsigned to_read = MIN(packet_length, 4096);
                            reader->read_bytes(reader, buffer, to_read);
                            buf_write(packet_data, buffer, to_read);
                            packet_length -= to_read;
                        }

                        audio_packet_found = 1;
                    } else {
                        /*other packets are ignored*/
                        reader->skip_bytes(reader, packet_length);
                    }
                }

                /*return success if an audio packet was read*/
                br_etry(reader);
                if (audio_packet_found) {
                    return 0;
                } else {
                    return 1;
                }
            } else {
                /*error reading sector*/
                br_etry(reader);
                return 1;
            }
        } else {
            /*error reading sector*/
            return 1;
        }
    } else {
        /*no more sectors, so return EOF*/
        return 0;
    }
}