static const uint8_t START_CODE[START_CODE_LEN] = { 0, 0, 0, 1 }; // start_code is 0x00000001
+static bool __is_valid_heif_buffer(heif_buffer_t *heif_buffer);
static gint __compare_item_type(gconstpointer a, gconstpointer b);
static gint __compare_image_item_id(gconstpointer data, gconstpointer user_data);
static gint __compare_iloc_item_id(gconstpointer data, gconstpointer user_data);
int heif_itemtable_get_coded_data(heif_itemtable_h handle, heif_image_item_h image_item, heif_buffer_t **coded_data)
{
_image_item_t *_image_item = (_image_item_t *)image_item;
- heif_buffer_t *nal_unit = NULL;
+ heif_buffer_t *codec_config = NULL;
heif_buffer_t *mdat = NULL;
size_t read_n = 0;
heif_retvm_if_failed(image_item, LIBHEIF_ERROR_INVALID_PARAMETER, "invalid image_item");
heif_retvm_if_failed(coded_data, LIBHEIF_ERROR_INVALID_PARAMETER, "invalid coded_data");
- nal_unit = g_new0(heif_buffer_t, 1);
- nal_unit->data = _image_item->hvc_config.nal_unit.data;
- nal_unit->size = _image_item->hvc_config.nal_unit.size;
+ codec_config = &(_image_item->hvc_config.nal_unit);
+ heif_retvm_if_failed(__is_valid_heif_buffer(codec_config), LIBHEIF_ERROR_INVALID_PARAMETER, "invalid codec_config");
- if (!nal_unit->data || nal_unit->size == 0) {
- heif_error("nal_unit: %p, nal_unit: %zu", nal_unit->data, nal_unit->size);
- g_free(nal_unit);
- return LIBHEIF_ERROR_INVALID_PARAMETER;
- }
#ifdef __DEBUG_CODEC_CONFIG
{
size_t i = 0;
- for (i = 0; i < nal_unit->size; i++)
- heif_error("nal_unit: %x", nal_unit->data[i]);
+ for (i = 0; i < codec_config->size; i++)
+ heif_error("codec_config: %x", codec_config->data[i]);
}
#endif
mdat = g_new0(heif_buffer_t, 1);
- mdat->size = nal_unit->size + _image_item->size;
+ mdat->size = codec_config->size + _image_item->size;
mdat->data = (uint8_t *)g_malloc0(mdat->size);
- // attach nal_unit in front of media
- memcpy(mdat->data, nal_unit->data, nal_unit->size);
- memcpy(mdat->data + nal_unit->size, START_CODE, START_CODE_LEN);
+ // put the codec_config and the start_code in front of media
+ memcpy(mdat->data, codec_config->data, codec_config->size);
+ memcpy(mdat->data + codec_config->size, START_CODE, START_CODE_LEN);
read_n = _image_item->size - START_CODE_LEN;
// read media
if (heif_source_read_at(((heif_itemtable_t *)handle)->source,
_image_item->offset + START_CODE_LEN,
- mdat->data + nal_unit->size + START_CODE_LEN,
+ mdat->data + codec_config->size + START_CODE_LEN,
read_n) != read_n) {
heif_error("heif_source_read_at fail");
heif_buffer_destroy(mdat);
g_free(buffer);
}
+static bool __is_valid_heif_buffer(heif_buffer_t *heif_buffer)
+{
+ heif_retvm_if_failed(heif_buffer, false, "invalid heif_buffer");
+ heif_retvm_if_failed(heif_buffer->data, false, "invalid heif_buffer data");
+ heif_retvm_if_failed(heif_buffer->size > 0, false, "invalid heif_buffer size");
+
+ return true;
+}
+
static gint __compare_item_type(gconstpointer a, gconstpointer b)
{
uint32_t item_type = (uint32_t)GPOINTER_TO_UINT(a);