2018-04-14 20:29:31 +00:00
|
|
|
/*
|
|
|
|
* AV1 common parsing code
|
|
|
|
*
|
|
|
|
* This file is part of FFmpeg.
|
|
|
|
*
|
|
|
|
* FFmpeg 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 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* FFmpeg 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#include "libavutil/mem.h"
|
|
|
|
|
2018-08-16 02:31:04 +00:00
|
|
|
#include "av1.h"
|
2018-04-14 20:29:31 +00:00
|
|
|
#include "av1_parse.h"
|
|
|
|
#include "bytestream.h"
|
|
|
|
|
|
|
|
int ff_av1_extract_obu(AV1OBU *obu, const uint8_t *buf, int length, void *logctx)
|
|
|
|
{
|
|
|
|
int64_t obu_size;
|
|
|
|
int start_pos, type, temporal_id, spatial_id;
|
2018-08-16 02:31:04 +00:00
|
|
|
int len;
|
2018-04-14 20:29:31 +00:00
|
|
|
|
2018-07-30 18:14:24 +00:00
|
|
|
len = parse_obu_header(buf, length, &obu_size, &start_pos,
|
|
|
|
&type, &temporal_id, &spatial_id);
|
|
|
|
if (len < 0)
|
|
|
|
return len;
|
2018-04-14 20:29:31 +00:00
|
|
|
|
|
|
|
obu->type = type;
|
|
|
|
obu->temporal_id = temporal_id;
|
|
|
|
obu->spatial_id = spatial_id;
|
|
|
|
|
|
|
|
obu->data = buf + start_pos;
|
|
|
|
obu->size = obu_size;
|
|
|
|
obu->raw_data = buf;
|
2018-07-30 18:14:24 +00:00
|
|
|
obu->raw_size = len;
|
2018-04-14 20:29:31 +00:00
|
|
|
|
|
|
|
av_log(logctx, AV_LOG_DEBUG,
|
|
|
|
"obu_type: %d, temporal_id: %d, spatial_id: %d, payload size: %d\n",
|
|
|
|
obu->type, obu->temporal_id, obu->spatial_id, obu->size);
|
|
|
|
|
2018-07-30 18:14:24 +00:00
|
|
|
return len;
|
2018-04-14 20:29:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int ff_av1_packet_split(AV1Packet *pkt, const uint8_t *buf, int length, void *logctx)
|
|
|
|
{
|
|
|
|
GetByteContext bc;
|
2018-08-16 02:31:04 +00:00
|
|
|
int ret, consumed;
|
2018-04-14 20:29:31 +00:00
|
|
|
|
|
|
|
bytestream2_init(&bc, buf, length);
|
|
|
|
pkt->nb_obus = 0;
|
|
|
|
|
|
|
|
while (bytestream2_get_bytes_left(&bc) > 0) {
|
|
|
|
AV1OBU *obu;
|
|
|
|
|
|
|
|
if (pkt->obus_allocated < pkt->nb_obus + 1) {
|
|
|
|
int new_size = pkt->obus_allocated + 1;
|
2019-10-15 02:42:01 +00:00
|
|
|
AV1OBU *tmp;
|
|
|
|
|
|
|
|
if (new_size >= INT_MAX / sizeof(*tmp))
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
tmp = av_fast_realloc(pkt->obus, &pkt->obus_allocated_size, new_size * sizeof(*tmp));
|
2018-04-14 20:29:31 +00:00
|
|
|
if (!tmp)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
pkt->obus = tmp;
|
2019-10-15 02:43:08 +00:00
|
|
|
memset(pkt->obus + pkt->obus_allocated, 0, sizeof(*pkt->obus));
|
2018-04-14 20:29:31 +00:00
|
|
|
pkt->obus_allocated = new_size;
|
|
|
|
}
|
|
|
|
obu = &pkt->obus[pkt->nb_obus];
|
|
|
|
|
|
|
|
consumed = ff_av1_extract_obu(obu, bc.buffer, bytestream2_get_bytes_left(&bc), logctx);
|
|
|
|
if (consumed < 0)
|
|
|
|
return consumed;
|
|
|
|
|
2018-08-16 02:31:04 +00:00
|
|
|
bytestream2_skip(&bc, consumed);
|
|
|
|
|
|
|
|
obu->size_bits = get_obu_bit_length(obu->data, obu->size, obu->type);
|
|
|
|
|
|
|
|
if (obu->size_bits < 0 || (!obu->size_bits && obu->type != AV1_OBU_TEMPORAL_DELIMITER)) {
|
|
|
|
av_log(logctx, AV_LOG_ERROR, "Invalid OBU of type %d, skipping.\n", obu->type);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2018-04-14 20:29:31 +00:00
|
|
|
pkt->nb_obus++;
|
|
|
|
|
2018-08-16 02:31:04 +00:00
|
|
|
ret = init_get_bits(&obu->gb, obu->data, obu->size_bits);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2018-04-14 20:29:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ff_av1_packet_uninit(AV1Packet *pkt)
|
|
|
|
{
|
|
|
|
av_freep(&pkt->obus);
|
2019-10-15 02:42:01 +00:00
|
|
|
pkt->obus_allocated = pkt->obus_allocated_size = 0;
|
2018-04-14 20:29:31 +00:00
|
|
|
}
|