Esempio n. 1
0
int
FlacDecoder_init(decoders_FlacDecoder *self,
                 PyObject *args, PyObject *kwds)
{
    self->file = NULL;
    self->bitstream = NULL;

    self->seektable = a_obj_new((ARRAY_COPY_FUNC)seekpoint_copy,
                                free,
                                NULL);

    self->subframe_data = aa_int_new();
    self->residuals = a_int_new();
    self->qlp_coeffs = a_int_new();
    self->framelist_data = a_int_new();
    self->audiotools_pcm = NULL;
    self->remaining_samples = 0;

    if (!PyArg_ParseTuple(args, "O", &self->file)) {
        return -1;
    } else {
        Py_INCREF(self->file);
    }

    /*open BitstreamReader from FLAC file stream
      based on whether it's a low-level file object*/
    if (PyFile_Check(self->file)) {
        /*open bitstream through file object*/
        self->bitstream = br_open(PyFile_AsFile(self->file), BS_BIG_ENDIAN);
    } else {
        /*treat file as Python-implemented file-like object*/
        self->bitstream = br_open_external(
            self->file,
            BS_BIG_ENDIAN,
            4096,
            (ext_read_f)br_read_python,
            (ext_close_f)bs_close_python,
            (ext_free_f)bs_free_python_nodecref);
    }

    /*read the STREAMINFO block, SEEKTABLE block
      and setup the total number of samples to read*/
    if (flacdec_read_metadata(self->bitstream,
                              &(self->streaminfo),
                              self->seektable,
                              &(self->channel_mask))) {
        self->streaminfo.channels = 0;
        return -1;
    }

    if (PyFile_Check(self->file)) {
        /*place mark at beginning of stream but after metadata
          in case seeking is needed*/
        self->bitstream->mark(self->bitstream);
    }

    self->remaining_samples = self->streaminfo.total_samples;

    /*initialize the output MD5 sum*/
    audiotools__MD5Init(&(self->md5));
    self->perform_validation = 1;
    self->stream_finalized = 0;

    /*setup a framelist generator function*/
    if ((self->audiotools_pcm = open_audiotools_pcm()) == NULL)
        return -1;

    /*mark stream as not closed and ready for reading*/
    self->closed = 0;

    return 0;
}
Esempio n. 2
0
static DVDA_Sector_Reader*
open_sector_reader(const char* audio_ts_path,
                   unsigned titleset_number,
                   const char* cdrom_device)
{
    DVDA_Sector_Reader* reader = malloc(sizeof(DVDA_Sector_Reader));
    unsigned i;

    reader->aobs = a_obj_new(NULL, (ARRAY_FREE_FUNC)free_aob, NULL);
#ifdef HAS_UNPROT
    reader->cppm_decoder = NULL;
#endif

    for (i = 1; i <= 9; i++) {
        char aob[13];
        char* path;

        snprintf(aob, 13, "ATS_%2.2d_%d.AOB", titleset_number, i);

        if ((path = find_audio_ts_file(audio_ts_path, aob)) != NULL) {
            struct stat aob_stat;
            if (!stat(path, &aob_stat)) {
                DVDA_AOB* aob = malloc(sizeof(DVDA_AOB));
                aob->path = path;
                if ((aob->file = fopen(aob->path, "rb")) == NULL) {
                    /*error opening AOB for reading*/
                    free(aob);
                    close_sector_reader(reader);
                    return NULL;
                }
                aob->total_sectors = (unsigned)(aob_stat.st_size / SECTOR_SIZE);

                /*set AOBs first_sector and last_sector
                  relative to the previous AOB, if any*/
                if (reader->aobs->len) {
                    DVDA_AOB* last_aob = reader->aobs->_[reader->aobs->len - 1];
                    aob->start_sector = last_aob->end_sector + 1;
                } else {
                    aob->start_sector = 0;
                }
                aob->end_sector = aob->start_sector + (aob->total_sectors - 1);
                reader->end_sector = aob->end_sector;

                reader->aobs->append(reader->aobs, aob);
            } else {
                /*error getting stat of AOB,
                  free memory and return error*/
                close_sector_reader(reader);
                return NULL;
            }
        } else {
            /*AOB not found, so exit loop*/
            break;
        }
    }

    if (reader->aobs->len) {
        /*ran out of AOBs,
          set initial position to start of 0th sector*/

        reader->current.sector = 0;
        reader->current.aob = reader->aobs->_[0];

#ifdef HAS_UNPROT
        /*if cdrom device given, initialize CPPM decoder if possible*/
        if (cdrom_device != NULL) {
            char* dvdaudio_mkb = find_audio_ts_file(audio_ts_path,
                                                    "DVDAUDIO.MKB");
            if (dvdaudio_mkb != NULL) {
                reader->cppm_decoder =
                    malloc(sizeof(struct cppm_decoder));
                reader->cppm_decoder->media_type = 0;
                reader->cppm_decoder->media_key = 0;
                reader->cppm_decoder->id_album_media = 0;

                switch (cppm_init(reader->cppm_decoder,
                                  cdrom_device,
                                  dvdaudio_mkb)) {
                case -1: /* I/O error */
                    free(dvdaudio_mkb);
                    close_sector_reader(reader);
                    return NULL;
                case -2: /* unsupported protection type */
                    free(dvdaudio_mkb);
                    close_sector_reader(reader);
                    fprintf(stderr, "unsupported protection type\n");
                    errno = ENOENT;
                    return NULL;
                default: /* all okay */
                    free(dvdaudio_mkb);
                    break;
                }
            }
        }
#endif

        return reader;
    } else {
        /*couldn't find any matching AOB files for titleset*/
        close_sector_reader(reader);
        errno = ENOENT;
        return NULL;
    }
}