Skip to content
C_RFC_API.cpp 64 KiB
Newer Older
Luker's avatar
Luker committed
/*
Luker's avatar
Luker committed
 * Copyright (c) 2015-2016, Luca Fulchir<luca@fulchir.it>, All rights reserved.
Luker's avatar
Luker committed
 *
 * This file is part of "libRaptorQ".
 *
 * libRaptorQ is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3
 * of the License, or (at your option) any later version.
 *
 * libRaptorQ is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * and a copy of the GNU Lesser General Public License
 * along with libRaptorQ.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "RaptorQ/v1/RFC.hpp"
Luker's avatar
Luker committed
#include "RaptorQ/v1/wrapper/C_RFC_API.h"
#include "RaptorQ/v1/caches.hpp"
Luker's avatar
Luker committed
#include "RaptorQ/v1/RFC.hpp"
#include <chrono>
Luker's avatar
Luker committed
#include <future>
Luker's avatar
Luker committed
#include <memory>

Luker's avatar
Luker committed
struct RAPTORQ_LOCAL RFC6330_ptr
Luker's avatar
Luker committed
{
Luker's avatar
Luker committed
    void *const ptr;
    const RFC6330_type type;
Luker's avatar
Luker committed

Luker's avatar
Luker committed
    RFC6330_ptr ()
        : ptr (nullptr), type (RFC6330_type::RQ_NONE) {}
    RFC6330_ptr (const RFC6330_type _type, void *const _ptr)
        : ptr (_ptr), type (_type) {}
Luker's avatar
Luker committed
};

Luker's avatar
Luker committed
struct RAPTORQ_LOCAL RFC6330_future
Luker's avatar
Luker committed
    std::future<std::pair<RFC6330__v1::Error, uint8_t>> f;
Luker's avatar
Luker committed
    RFC6330_future(){}
Luker's avatar
Luker committed

////////////////////////
////
//// For ease of development please keep the function declaration
//// and implementation in the same orders as defined in the structs!
////
////////////////////////

// precomputation caching
static RFC6330_Compress RAPTORQ_LOCAL v1_supported_compressions();
static RFC6330_Compress RAPTORQ_LOCAL v1_get_compression();
static bool RAPTORQ_LOCAL v1_set_compression (
                                            const RFC6330_Compress compression);
static size_t RAPTORQ_LOCAL v1_shared_cache_size (const size_t shared_cache);
static size_t RAPTORQ_LOCAL v1_local_cache_size (const size_t local_cache);
static size_t RAPTORQ_LOCAL v1_get_shared_cache_size ();
static size_t RAPTORQ_LOCAL v1_get_local_cache_size ();
// constructors
struct RFC6330_ptr* RAPTORQ_LOCAL v1_Encoder (RFC6330_type type,
                                              const void *data_from,
                                              const size_t size,
                                              const uint16_t min_subsymbol_size,
                                              const uint16_t symbol_size,
                                              const size_t max_memory);
struct RFC6330_ptr* RAPTORQ_LOCAL v1_Decoder (RFC6330_type type,
                              const RFC6330_OTI_Common_Data common,
                              const RFC6330_OTI_Scheme_Specific_Data scheme);
struct RFC6330_ptr* RAPTORQ_LOCAL v1_Decoder_raw (RFC6330_type type,
                                                    const uint64_t size,
                                                    const uint16_t symbol_size,
                                                    const uint16_t sub_blocks,
                                                    const uint8_t blocks,
                                                    const uint8_t alignment);
static bool RAPTORQ_LOCAL v1_initialized (const struct RFC6330_ptr *ptr);


// common functions
uint8_t RAPTORQ_LOCAL v1_blocks (const struct RFC6330_ptr *ptr);
uint16_t RAPTORQ_LOCAL v1_symbols (const struct RFC6330_ptr *ptr,
                                                            const uint8_t sbn);
size_t RAPTORQ_LOCAL v1_symbol_size (const struct RFC6330_ptr *ptr);
RaptorQ_Error RAPTORQ_LOCAL v1_future_state (const struct RFC6330_future *f);
RaptorQ_Error RAPTORQ_LOCAL v1_future_wait_for (const struct RFC6330_future *f,
                                                const uint64_t time,
                                                const RFC6330_Unit_Time unit);
void RAPTORQ_LOCAL v1_future_wait (const struct RFC6330_future *f);
void RAPTORQ_LOCAL v1_future_free (struct RFC6330_future **f);
struct RFC6330_Result RAPTORQ_LOCAL v1_future_get (
                                                struct RFC6330_future *future);
bool RAPTORQ_LOCAL v1_set_thread_pool (const size_t threads,
                                const uint16_t max_block_concurrency,
                                const RFC6330_Work exit_type);
struct RFC6330_future* RAPTORQ_LOCAL v1_compute (const struct RFC6330_ptr *ptr,
                                                const RFC6330_Compute flags);
void RAPTORQ_LOCAL v1_free (struct RFC6330_ptr **ptr);
void RAPTORQ_LOCAL v1_free_block (const struct RFC6330_ptr *ptr,
                                                            const uint8_t sbn);


// encoder-specific
RFC6330_OTI_Common_Data RAPTORQ_LOCAL v1_OTI_Common (
                                                const struct RFC6330_ptr *enc);
RFC6330_OTI_Scheme_Specific_Data RAPTORQ_LOCAL v1_OTI_Scheme_Specific (
                                                const struct RFC6330_ptr *enc);
uint32_t RAPTORQ_LOCAL v1_max_repair (const struct RFC6330_ptr *enc,
                                                            const uint8_t sbn);
size_t RAPTORQ_LOCAL v1_precompute_max_memory (const struct RFC6330_ptr *enc);

size_t RAPTORQ_LOCAL v1_encode_id (const struct RFC6330_ptr *enc, void **data,
                                                            const size_t size,
                                                            const uint32_t id);
size_t RAPTORQ_LOCAL v1_encode (const struct RFC6330_ptr *enc, void **data,
                                                            const size_t size,
                                                            const uint32_t esi,
                                                            const uint8_t sbn);
uint32_t RAPTORQ_LOCAL v1_id (const uint32_t esi, const uint8_t sbn);


// decoder-specific
void RAPTORQ_LOCAL v1_end_of_input (const struct RFC6330_ptr *dec);
void RAPTORQ_LOCAL v1_end_of_block_input (const struct RFC6330_ptr *dec,
                                                        const uint8_t block);
uint64_t RAPTORQ_LOCAL v1_bytes (const struct RFC6330_ptr *dec);
struct RFC6330_Dec_Result RAPTORQ_LOCAL v1_decode_aligned (
                                                const struct RFC6330_ptr *dec,
                                                void **data,
                                                const uint64_t size,
                                                const uint8_t skip);
struct RFC6330_Dec_Result RAPTORQ_LOCAL v1_decode_block_aligned (
                                                const struct RFC6330_ptr *dec,
                                                void **data,
                                                const size_t size,
                                                const uint8_t skip,
                                                const uint8_t sbn);
uint64_t RAPTORQ_LOCAL v1_decode_bytes (const struct RFC6330_ptr *dec,
                                                            void **data,
                                                            const uint64_t size,
                                                            const uint8_t skip);
size_t RAPTORQ_LOCAL v1_decode_block_bytes (const struct RFC6330_ptr *dec,
                                                            void **data,
                                                            const size_t size,
                                                            const uint8_t skip,
                                                            const uint8_t sbn);
RFC6330_Error RAPTORQ_LOCAL v1_add_symbol_id (const struct RFC6330_ptr *dec,
                                                            void **data,
                                                            const uint32_t size,
                                                            const uint32_t id);
RFC6330_Error RAPTORQ_LOCAL v1_add_symbol (const struct RFC6330_ptr *dec,
                                                            void **data,
                                                            const uint32_t size,
                                                            const uint32_t esi,
                                                            const uint8_t sbn);



void RFC6330_free_api (struct RFC6330_base_api **api)
Luker's avatar
Luker committed
{
Luker's avatar
Luker committed
    if (api == nullptr || *api == nullptr)
Luker's avatar
Luker committed
        return;
Luker's avatar
Luker committed
    if ((*api)->version == 1) {
        delete reinterpret_cast<struct RFC6330_v1*> (*api);
    } // else it's all your fault anway
    *api = nullptr;
Luker's avatar
Luker committed
}

Luker's avatar
Luker committed
struct RFC6330_base_api* RFC6330_api (uint32_t version)
Luker's avatar
Luker committed
{
Luker's avatar
Luker committed
    if (version != 1)
        return nullptr;
    return reinterpret_cast<RFC6330_base_api *> (new RFC6330_v1());
Luker's avatar
Luker committed
}

Luker's avatar
Luker committed
RFC6330_v1::RFC6330_v1()
    : base (1),
    // precomputation caching
    supported_compressions (&v1_supported_compressions),
    get_compression (&v1_get_compression),
    set_compression (&v1_set_compression),
    shared_cache_size (&v1_shared_cache_size),
    local_cache_size (&v1_local_cache_size),
    get_shared_cache_size (&v1_get_shared_cache_size),
    get_local_cache_size (&v1_get_local_cache_size),

    // constructors
    Encoder (&v1_Encoder),
    Decoder (&v1_Decoder),
    Decoder_raw (&v1_Decoder_raw),
    initialized (&v1_initialized),

    // common functions
    blocks (&v1_blocks),
    symbols (&v1_symbols),
    symbol_size (&v1_symbol_size),
    future_state (&v1_future_state),
    future_wait_for (&v1_future_wait_for),
Loading full blame...