2009-04-07 21:31:14 +03:00
|
|
|
/*
|
|
|
|
* AVPacket functions for libavcodec
|
|
|
|
* Copyright (c) 2000, 2001, 2002 Fabrice Bellard
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2012-08-06 16:49:32 +03:00
|
|
|
#include <string.h>
|
|
|
|
|
2010-11-21 12:21:06 +02:00
|
|
|
#include "libavutil/avassert.h"
|
2021-07-23 14:06:58 +02:00
|
|
|
#include "libavutil/avutil.h"
|
2022-02-04 16:17:19 +02:00
|
|
|
#include "libavutil/intreadwrite.h"
|
2014-06-18 10:39:32 +03:00
|
|
|
#include "libavutil/mathematics.h"
|
2012-08-06 16:49:32 +03:00
|
|
|
#include "libavutil/mem.h"
|
2021-01-23 20:56:18 +02:00
|
|
|
#include "libavutil/rational.h"
|
2020-04-07 16:41:48 +02:00
|
|
|
|
2022-02-04 16:17:19 +02:00
|
|
|
#include "defs.h"
|
2020-04-07 16:41:48 +02:00
|
|
|
#include "packet.h"
|
2020-06-02 23:38:33 +02:00
|
|
|
#include "packet_internal.h"
|
2009-04-07 21:31:14 +03:00
|
|
|
|
2021-01-27 21:24:10 +02:00
|
|
|
#if FF_API_INIT_PACKET
|
2009-04-07 21:31:14 +03:00
|
|
|
void av_init_packet(AVPacket *pkt)
|
|
|
|
{
|
2012-04-01 11:47:39 +03:00
|
|
|
pkt->pts = AV_NOPTS_VALUE;
|
|
|
|
pkt->dts = AV_NOPTS_VALUE;
|
|
|
|
pkt->pos = -1;
|
|
|
|
pkt->duration = 0;
|
|
|
|
pkt->flags = 0;
|
|
|
|
pkt->stream_index = 0;
|
2012-10-31 10:53:18 +03:00
|
|
|
pkt->buf = NULL;
|
2012-04-01 11:47:39 +03:00
|
|
|
pkt->side_data = NULL;
|
|
|
|
pkt->side_data_elems = 0;
|
2021-01-23 20:56:18 +02:00
|
|
|
pkt->opaque = NULL;
|
|
|
|
pkt->opaque_ref = NULL;
|
2021-08-02 14:40:57 +02:00
|
|
|
pkt->time_base = av_make_q(0, 1);
|
2009-04-07 21:31:14 +03:00
|
|
|
}
|
2021-01-27 21:24:10 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
static void get_packet_defaults(AVPacket *pkt)
|
|
|
|
{
|
|
|
|
memset(pkt, 0, sizeof(*pkt));
|
|
|
|
|
|
|
|
pkt->pts = AV_NOPTS_VALUE;
|
|
|
|
pkt->dts = AV_NOPTS_VALUE;
|
|
|
|
pkt->pos = -1;
|
2021-08-02 14:40:57 +02:00
|
|
|
pkt->time_base = av_make_q(0, 1);
|
2021-01-27 21:24:10 +02:00
|
|
|
}
|
2009-04-07 21:31:14 +03:00
|
|
|
|
2015-10-23 11:11:34 +02:00
|
|
|
AVPacket *av_packet_alloc(void)
|
|
|
|
{
|
2021-05-08 23:36:28 +02:00
|
|
|
AVPacket *pkt = av_malloc(sizeof(AVPacket));
|
2015-10-23 11:11:34 +02:00
|
|
|
if (!pkt)
|
|
|
|
return pkt;
|
|
|
|
|
2021-01-27 21:24:10 +02:00
|
|
|
get_packet_defaults(pkt);
|
2015-10-23 11:11:34 +02:00
|
|
|
|
|
|
|
return pkt;
|
|
|
|
}
|
|
|
|
|
|
|
|
void av_packet_free(AVPacket **pkt)
|
|
|
|
{
|
|
|
|
if (!pkt || !*pkt)
|
|
|
|
return;
|
|
|
|
|
|
|
|
av_packet_unref(*pkt);
|
|
|
|
av_freep(pkt);
|
|
|
|
}
|
|
|
|
|
2013-07-10 11:52:56 +03:00
|
|
|
static int packet_alloc(AVBufferRef **buf, int size)
|
2009-04-07 21:31:14 +03:00
|
|
|
{
|
2013-09-11 23:09:37 +03:00
|
|
|
int ret;
|
2016-01-05 14:01:53 +02:00
|
|
|
if (size < 0 || size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
|
2012-10-31 10:53:18 +03:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
2015-06-29 23:48:34 +02:00
|
|
|
ret = av_buffer_realloc(buf, size + AV_INPUT_BUFFER_PADDING_SIZE);
|
2013-09-11 23:09:37 +03:00
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2012-10-31 10:53:18 +03:00
|
|
|
|
2015-06-29 23:48:34 +02:00
|
|
|
memset((*buf)->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
|
2013-07-10 11:52:56 +03:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int av_new_packet(AVPacket *pkt, int size)
|
|
|
|
{
|
|
|
|
AVBufferRef *buf = NULL;
|
|
|
|
int ret = packet_alloc(&buf, size);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2009-04-07 21:31:14 +03:00
|
|
|
|
2021-01-27 21:24:10 +02:00
|
|
|
get_packet_defaults(pkt);
|
2012-10-31 10:53:18 +03:00
|
|
|
pkt->buf = buf;
|
|
|
|
pkt->data = buf->data;
|
2012-04-01 11:47:39 +03:00
|
|
|
pkt->size = size;
|
2012-10-31 10:53:18 +03:00
|
|
|
|
2009-04-07 21:31:14 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-04-08 23:19:12 +03:00
|
|
|
void av_shrink_packet(AVPacket *pkt, int size)
|
|
|
|
{
|
2012-04-01 11:47:39 +03:00
|
|
|
if (pkt->size <= size)
|
|
|
|
return;
|
2009-04-08 23:19:12 +03:00
|
|
|
pkt->size = size;
|
2015-06-29 23:48:34 +02:00
|
|
|
memset(pkt->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
|
2009-04-08 23:19:12 +03:00
|
|
|
}
|
|
|
|
|
2010-11-21 12:21:06 +02:00
|
|
|
int av_grow_packet(AVPacket *pkt, int grow_by)
|
|
|
|
{
|
2012-10-31 10:53:18 +03:00
|
|
|
int new_size;
|
2015-06-29 23:48:34 +02:00
|
|
|
av_assert0((unsigned)pkt->size <= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
|
2012-04-01 11:47:39 +03:00
|
|
|
if ((unsigned)grow_by >
|
2015-06-29 23:48:34 +02:00
|
|
|
INT_MAX - (pkt->size + AV_INPUT_BUFFER_PADDING_SIZE))
|
2018-12-31 19:25:18 +02:00
|
|
|
return AVERROR(ENOMEM);
|
2012-10-31 10:53:18 +03:00
|
|
|
|
2015-06-29 23:48:34 +02:00
|
|
|
new_size = pkt->size + grow_by + AV_INPUT_BUFFER_PADDING_SIZE;
|
2012-10-31 10:53:18 +03:00
|
|
|
if (pkt->buf) {
|
2016-05-25 16:56:21 +02:00
|
|
|
size_t data_offset;
|
|
|
|
uint8_t *old_data = pkt->data;
|
|
|
|
if (pkt->data == NULL) {
|
|
|
|
data_offset = 0;
|
|
|
|
pkt->data = pkt->buf->data;
|
|
|
|
} else {
|
|
|
|
data_offset = pkt->data - pkt->buf->data;
|
|
|
|
if (data_offset > INT_MAX - new_size)
|
2018-12-31 19:25:18 +02:00
|
|
|
return AVERROR(ENOMEM);
|
2016-05-25 16:56:21 +02:00
|
|
|
}
|
|
|
|
|
2020-02-12 14:27:17 +02:00
|
|
|
if (new_size + data_offset > pkt->buf->size ||
|
|
|
|
!av_buffer_is_writable(pkt->buf)) {
|
2021-12-04 21:33:30 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
// allocate slightly more than requested to avoid excessive
|
|
|
|
// reallocations
|
|
|
|
if (new_size + data_offset < INT_MAX - new_size/16)
|
|
|
|
new_size += new_size/16;
|
|
|
|
|
|
|
|
ret = av_buffer_realloc(&pkt->buf, new_size + data_offset);
|
2016-05-25 16:56:21 +02:00
|
|
|
if (ret < 0) {
|
|
|
|
pkt->data = old_data;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
pkt->data = pkt->buf->data + data_offset;
|
|
|
|
}
|
2012-10-31 10:53:18 +03:00
|
|
|
} else {
|
|
|
|
pkt->buf = av_buffer_alloc(new_size);
|
|
|
|
if (!pkt->buf)
|
|
|
|
return AVERROR(ENOMEM);
|
2016-09-22 01:03:55 +02:00
|
|
|
if (pkt->size > 0)
|
|
|
|
memcpy(pkt->buf->data, pkt->data, pkt->size);
|
2016-05-25 16:56:21 +02:00
|
|
|
pkt->data = pkt->buf->data;
|
2012-10-31 10:53:18 +03:00
|
|
|
}
|
2010-11-21 12:21:06 +02:00
|
|
|
pkt->size += grow_by;
|
2015-06-29 23:48:34 +02:00
|
|
|
memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
|
2012-10-31 10:53:18 +03:00
|
|
|
|
2010-11-21 12:21:06 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-10-31 10:53:18 +03:00
|
|
|
int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
|
|
|
|
{
|
2015-06-29 23:48:34 +02:00
|
|
|
if (size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
|
2012-10-31 10:53:18 +03:00
|
|
|
return AVERROR(EINVAL);
|
|
|
|
|
2015-06-29 23:48:34 +02:00
|
|
|
pkt->buf = av_buffer_create(data, size + AV_INPUT_BUFFER_PADDING_SIZE,
|
2012-10-31 10:53:18 +03:00
|
|
|
av_buffer_default_free, NULL, 0);
|
|
|
|
if (!pkt->buf)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
pkt->data = data;
|
|
|
|
pkt->size = size;
|
|
|
|
|
2010-11-21 12:21:06 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-10 11:52:56 +03:00
|
|
|
void av_packet_free_side_data(AVPacket *pkt)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < pkt->side_data_elems; i++)
|
2014-10-07 04:40:57 +03:00
|
|
|
av_freep(&pkt->side_data[i].data);
|
2013-07-10 11:52:56 +03:00
|
|
|
av_freep(&pkt->side_data);
|
|
|
|
pkt->side_data_elems = 0;
|
|
|
|
}
|
|
|
|
|
2015-10-04 12:08:20 +02:00
|
|
|
int av_packet_add_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
|
|
|
|
uint8_t *data, size_t size)
|
2011-04-09 16:31:39 +03:00
|
|
|
{
|
2016-11-19 17:38:44 +02:00
|
|
|
AVPacketSideData *tmp;
|
2017-05-12 18:45:44 +02:00
|
|
|
int i, elems = pkt->side_data_elems;
|
|
|
|
|
|
|
|
for (i = 0; i < elems; i++) {
|
|
|
|
AVPacketSideData *sd = &pkt->side_data[i];
|
|
|
|
|
|
|
|
if (sd->type == type) {
|
|
|
|
av_free(sd->data);
|
|
|
|
sd->data = data;
|
|
|
|
sd->size = size;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2011-04-09 16:31:39 +03:00
|
|
|
|
2017-05-11 13:01:36 +02:00
|
|
|
if ((unsigned)elems + 1 > AV_PKT_DATA_NB)
|
2015-12-06 11:59:51 +02:00
|
|
|
return AVERROR(ERANGE);
|
2011-04-09 16:31:39 +03:00
|
|
|
|
2016-11-19 17:38:44 +02:00
|
|
|
tmp = av_realloc(pkt->side_data, (elems + 1) * sizeof(*tmp));
|
|
|
|
if (!tmp)
|
2015-10-04 12:08:20 +02:00
|
|
|
return AVERROR(ENOMEM);
|
2011-04-09 16:31:39 +03:00
|
|
|
|
2016-11-19 17:38:44 +02:00
|
|
|
pkt->side_data = tmp;
|
2015-10-04 12:08:20 +02:00
|
|
|
pkt->side_data[elems].data = data;
|
2011-04-09 16:31:39 +03:00
|
|
|
pkt->side_data[elems].size = size;
|
|
|
|
pkt->side_data[elems].type = type;
|
|
|
|
pkt->side_data_elems++;
|
|
|
|
|
2015-10-04 12:08:20 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint8_t *av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
|
2021-04-14 14:59:32 +02:00
|
|
|
size_t size)
|
2015-10-04 12:08:20 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
uint8_t *data;
|
|
|
|
|
2021-03-06 17:43:52 +02:00
|
|
|
if (size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
|
2015-10-04 12:08:20 +02:00
|
|
|
return NULL;
|
2015-12-17 14:12:07 +02:00
|
|
|
data = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
|
2015-10-04 12:08:20 +02:00
|
|
|
if (!data)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
ret = av_packet_add_side_data(pkt, type, data, size);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_freep(&data);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
2011-04-09 16:31:39 +03:00
|
|
|
}
|
|
|
|
|
2017-02-26 12:58:49 +02:00
|
|
|
uint8_t *av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type,
|
2021-04-14 14:59:32 +02:00
|
|
|
size_t *size)
|
2011-04-09 16:31:39 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < pkt->side_data_elems; i++) {
|
|
|
|
if (pkt->side_data[i].type == type) {
|
|
|
|
if (size)
|
|
|
|
*size = pkt->side_data[i].size;
|
|
|
|
return pkt->side_data[i].data;
|
|
|
|
}
|
2009-04-30 15:06:34 +03:00
|
|
|
}
|
2017-02-17 01:46:14 +02:00
|
|
|
if (size)
|
|
|
|
*size = 0;
|
2011-04-09 16:31:39 +03:00
|
|
|
return NULL;
|
2009-04-30 15:06:34 +03:00
|
|
|
}
|
2011-04-20 04:25:48 +03:00
|
|
|
|
2015-05-03 03:21:23 +02:00
|
|
|
const char *av_packet_side_data_name(enum AVPacketSideDataType type)
|
|
|
|
{
|
|
|
|
switch(type) {
|
2016-02-23 00:53:06 +02:00
|
|
|
case AV_PKT_DATA_PALETTE: return "Palette";
|
|
|
|
case AV_PKT_DATA_NEW_EXTRADATA: return "New Extradata";
|
|
|
|
case AV_PKT_DATA_PARAM_CHANGE: return "Param Change";
|
|
|
|
case AV_PKT_DATA_H263_MB_INFO: return "H263 MB Info";
|
|
|
|
case AV_PKT_DATA_REPLAYGAIN: return "Replay Gain";
|
|
|
|
case AV_PKT_DATA_DISPLAYMATRIX: return "Display Matrix";
|
|
|
|
case AV_PKT_DATA_STEREO3D: return "Stereo 3D";
|
|
|
|
case AV_PKT_DATA_AUDIO_SERVICE_TYPE: return "Audio Service Type";
|
2018-09-17 16:27:31 +02:00
|
|
|
case AV_PKT_DATA_QUALITY_STATS: return "Quality stats";
|
|
|
|
case AV_PKT_DATA_FALLBACK_TRACK: return "Fallback track";
|
|
|
|
case AV_PKT_DATA_CPB_PROPERTIES: return "CPB properties";
|
2016-02-23 00:53:06 +02:00
|
|
|
case AV_PKT_DATA_SKIP_SAMPLES: return "Skip Samples";
|
|
|
|
case AV_PKT_DATA_JP_DUALMONO: return "JP Dual Mono";
|
|
|
|
case AV_PKT_DATA_STRINGS_METADATA: return "Strings Metadata";
|
|
|
|
case AV_PKT_DATA_SUBTITLE_POSITION: return "Subtitle Position";
|
|
|
|
case AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL: return "Matroska BlockAdditional";
|
|
|
|
case AV_PKT_DATA_WEBVTT_IDENTIFIER: return "WebVTT ID";
|
|
|
|
case AV_PKT_DATA_WEBVTT_SETTINGS: return "WebVTT Settings";
|
|
|
|
case AV_PKT_DATA_METADATA_UPDATE: return "Metadata Update";
|
|
|
|
case AV_PKT_DATA_MPEGTS_STREAM_ID: return "MPEGTS Stream ID";
|
|
|
|
case AV_PKT_DATA_MASTERING_DISPLAY_METADATA: return "Mastering display metadata";
|
2017-04-03 09:29:46 +02:00
|
|
|
case AV_PKT_DATA_CONTENT_LIGHT_LEVEL: return "Content light level metadata";
|
2016-11-12 00:37:43 +02:00
|
|
|
case AV_PKT_DATA_SPHERICAL: return "Spherical Mapping";
|
2017-08-30 08:13:16 +02:00
|
|
|
case AV_PKT_DATA_A53_CC: return "A53 Closed Captions";
|
2018-09-17 16:27:31 +02:00
|
|
|
case AV_PKT_DATA_ENCRYPTION_INIT_INFO: return "Encryption initialization data";
|
|
|
|
case AV_PKT_DATA_ENCRYPTION_INFO: return "Encryption info";
|
|
|
|
case AV_PKT_DATA_AFD: return "Active Format Description data";
|
2020-05-04 17:17:01 +02:00
|
|
|
case AV_PKT_DATA_PRFT: return "Producer Reference Time";
|
2019-09-23 22:43:02 +02:00
|
|
|
case AV_PKT_DATA_ICC_PROFILE: return "ICC Profile";
|
2020-04-11 15:07:15 +02:00
|
|
|
case AV_PKT_DATA_DOVI_CONF: return "DOVI configuration record";
|
2020-06-30 17:18:17 +02:00
|
|
|
case AV_PKT_DATA_S12M_TIMECODE: return "SMPTE ST 12-1:2014 timecode";
|
2021-06-18 07:20:44 +02:00
|
|
|
case AV_PKT_DATA_DYNAMIC_HDR10_PLUS: return "HDR10+ Dynamic Metadata (SMPTE 2094-40)";
|
2024-02-04 17:14:10 +02:00
|
|
|
case AV_PKT_DATA_AMBIENT_VIEWING_ENVIRONMENT:return "Ambient viewing environment";
|
2023-11-25 16:26:24 +02:00
|
|
|
case AV_PKT_DATA_IAMF_MIX_GAIN_PARAM: return "IAMF Mix Gain Parameter Data";
|
|
|
|
case AV_PKT_DATA_IAMF_DEMIXING_INFO_PARAM: return "IAMF Demixing Info Parameter Data";
|
|
|
|
case AV_PKT_DATA_IAMF_RECON_GAIN_INFO_PARAM: return "IAMF Recon Gain Info Parameter Data";
|
2015-05-03 03:21:23 +02:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-03-17 20:31:45 +02:00
|
|
|
uint8_t *av_packet_pack_dictionary(AVDictionary *dict, size_t *size)
|
2013-11-18 04:36:26 +03:00
|
|
|
{
|
|
|
|
uint8_t *data = NULL;
|
|
|
|
*size = 0;
|
|
|
|
|
|
|
|
if (!dict)
|
|
|
|
return NULL;
|
|
|
|
|
2021-03-17 19:32:36 +02:00
|
|
|
for (int pass = 0; pass < 2; pass++) {
|
|
|
|
const AVDictionaryEntry *t = NULL;
|
|
|
|
size_t total_length = 0;
|
2013-11-18 04:36:26 +03:00
|
|
|
|
2022-11-26 16:46:28 +02:00
|
|
|
while ((t = av_dict_iterate(dict, t))) {
|
2021-03-17 19:32:36 +02:00
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
const char *str = i ? t->value : t->key;
|
|
|
|
const size_t len = strlen(str) + 1;
|
2013-11-18 04:36:26 +03:00
|
|
|
|
2021-03-17 19:32:36 +02:00
|
|
|
if (pass)
|
|
|
|
memcpy(data + total_length, str, len);
|
2021-03-17 20:31:45 +02:00
|
|
|
else if (len > SIZE_MAX - total_length)
|
2021-03-17 19:32:36 +02:00
|
|
|
return NULL;
|
|
|
|
total_length += len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pass)
|
|
|
|
break;
|
|
|
|
data = av_malloc(total_length);
|
|
|
|
if (!data)
|
|
|
|
return NULL;
|
|
|
|
*size = total_length;
|
2013-11-18 04:36:26 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2021-03-17 20:31:45 +02:00
|
|
|
int av_packet_unpack_dictionary(const uint8_t *data, size_t size,
|
|
|
|
AVDictionary **dict)
|
2013-11-18 04:36:26 +03:00
|
|
|
{
|
2019-05-10 13:03:54 +02:00
|
|
|
const uint8_t *end;
|
2020-04-17 15:55:57 +02:00
|
|
|
int ret;
|
2013-11-18 04:36:26 +03:00
|
|
|
|
|
|
|
if (!dict || !data || !size)
|
2020-04-17 15:55:57 +02:00
|
|
|
return 0;
|
2019-05-10 13:03:54 +02:00
|
|
|
end = data + size;
|
2013-11-18 04:36:26 +03:00
|
|
|
if (size && end[-1])
|
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
while (data < end) {
|
|
|
|
const uint8_t *key = data;
|
|
|
|
const uint8_t *val = data + strlen(key) + 1;
|
|
|
|
|
2017-02-27 05:12:09 +02:00
|
|
|
if (val >= end || !*key)
|
2013-11-18 04:36:26 +03:00
|
|
|
return AVERROR_INVALIDDATA;
|
|
|
|
|
|
|
|
ret = av_dict_set(dict, key, val, 0);
|
|
|
|
if (ret < 0)
|
2020-04-17 15:55:57 +02:00
|
|
|
return ret;
|
2013-11-18 04:36:26 +03:00
|
|
|
data = val + strlen(val) + 1;
|
|
|
|
}
|
|
|
|
|
2020-04-17 15:55:57 +02:00
|
|
|
return 0;
|
2013-11-18 04:36:26 +03:00
|
|
|
}
|
|
|
|
|
2012-02-28 02:08:31 +03:00
|
|
|
int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
|
2021-04-14 14:59:32 +02:00
|
|
|
size_t size)
|
2012-02-28 02:08:31 +03:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < pkt->side_data_elems; i++) {
|
|
|
|
if (pkt->side_data[i].type == type) {
|
|
|
|
if (size > pkt->side_data[i].size)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
pkt->side_data[i].size = size;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return AVERROR(ENOENT);
|
|
|
|
}
|
2013-07-10 11:52:56 +03:00
|
|
|
|
|
|
|
int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
|
|
|
|
{
|
2021-01-23 20:56:18 +02:00
|
|
|
int i, ret;
|
2013-07-10 11:52:56 +03:00
|
|
|
|
|
|
|
dst->pts = src->pts;
|
|
|
|
dst->dts = src->dts;
|
|
|
|
dst->pos = src->pos;
|
|
|
|
dst->duration = src->duration;
|
|
|
|
dst->flags = src->flags;
|
|
|
|
dst->stream_index = src->stream_index;
|
2021-01-23 20:56:18 +02:00
|
|
|
dst->opaque = src->opaque;
|
|
|
|
dst->time_base = src->time_base;
|
|
|
|
dst->opaque_ref = NULL;
|
2018-02-14 04:43:24 +02:00
|
|
|
dst->side_data = NULL;
|
|
|
|
dst->side_data_elems = 0;
|
2021-01-23 20:56:18 +02:00
|
|
|
|
|
|
|
ret = av_buffer_replace(&dst->opaque_ref, src->opaque_ref);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
|
|
|
|
2013-07-10 11:52:56 +03:00
|
|
|
for (i = 0; i < src->side_data_elems; i++) {
|
2018-09-10 03:09:47 +02:00
|
|
|
enum AVPacketSideDataType type = src->side_data[i].type;
|
2021-04-14 14:59:32 +02:00
|
|
|
size_t size = src->side_data[i].size;
|
2018-09-10 03:09:47 +02:00
|
|
|
uint8_t *src_data = src->side_data[i].data;
|
|
|
|
uint8_t *dst_data = av_packet_new_side_data(dst, type, size);
|
2013-07-10 11:52:56 +03:00
|
|
|
|
|
|
|
if (!dst_data) {
|
2021-01-23 20:56:18 +02:00
|
|
|
av_buffer_unref(&dst->opaque_ref);
|
2013-07-10 11:52:56 +03:00
|
|
|
av_packet_free_side_data(dst);
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
}
|
|
|
|
memcpy(dst_data, src_data, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void av_packet_unref(AVPacket *pkt)
|
|
|
|
{
|
|
|
|
av_packet_free_side_data(pkt);
|
2021-01-23 20:56:18 +02:00
|
|
|
av_buffer_unref(&pkt->opaque_ref);
|
2013-07-10 11:52:56 +03:00
|
|
|
av_buffer_unref(&pkt->buf);
|
2021-01-27 21:24:10 +02:00
|
|
|
get_packet_defaults(pkt);
|
2013-07-10 11:52:56 +03:00
|
|
|
}
|
|
|
|
|
2014-04-03 13:45:35 +03:00
|
|
|
int av_packet_ref(AVPacket *dst, const AVPacket *src)
|
2013-07-10 11:52:56 +03:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2020-03-27 02:56:38 +02:00
|
|
|
dst->buf = NULL;
|
|
|
|
|
2013-07-10 11:52:56 +03:00
|
|
|
ret = av_packet_copy_props(dst, src);
|
|
|
|
if (ret < 0)
|
2020-03-27 02:56:38 +02:00
|
|
|
goto fail;
|
2013-07-10 11:52:56 +03:00
|
|
|
|
|
|
|
if (!src->buf) {
|
|
|
|
ret = packet_alloc(&dst->buf, src->size);
|
|
|
|
if (ret < 0)
|
|
|
|
goto fail;
|
2019-02-14 23:36:46 +02:00
|
|
|
av_assert1(!src->size || src->data);
|
2016-09-04 21:11:02 +02:00
|
|
|
if (src->size)
|
|
|
|
memcpy(dst->buf->data, src->data, src->size);
|
2016-05-12 15:34:58 +02:00
|
|
|
|
2016-05-12 16:47:09 +02:00
|
|
|
dst->data = dst->buf->data;
|
2014-12-12 19:15:42 +02:00
|
|
|
} else {
|
2013-07-10 11:52:56 +03:00
|
|
|
dst->buf = av_buffer_ref(src->buf);
|
2015-02-02 21:53:51 +02:00
|
|
|
if (!dst->buf) {
|
|
|
|
ret = AVERROR(ENOMEM);
|
2014-12-12 19:15:42 +02:00
|
|
|
goto fail;
|
2015-02-02 21:53:51 +02:00
|
|
|
}
|
2016-05-12 16:47:09 +02:00
|
|
|
dst->data = src->data;
|
2014-12-12 19:15:42 +02:00
|
|
|
}
|
2013-07-10 11:52:56 +03:00
|
|
|
|
|
|
|
dst->size = src->size;
|
2016-05-12 16:47:09 +02:00
|
|
|
|
2013-07-10 11:52:56 +03:00
|
|
|
return 0;
|
|
|
|
fail:
|
2020-03-27 02:56:38 +02:00
|
|
|
av_packet_unref(dst);
|
2013-07-10 11:52:56 +03:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2016-11-17 20:41:12 +02:00
|
|
|
AVPacket *av_packet_clone(const AVPacket *src)
|
2015-10-23 11:11:34 +02:00
|
|
|
{
|
|
|
|
AVPacket *ret = av_packet_alloc();
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (av_packet_ref(ret, src))
|
|
|
|
av_packet_free(&ret);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-07-10 11:52:56 +03:00
|
|
|
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
|
|
|
|
{
|
|
|
|
*dst = *src;
|
2021-01-27 21:24:10 +02:00
|
|
|
get_packet_defaults(src);
|
2013-07-10 11:52:56 +03:00
|
|
|
}
|
2014-06-18 10:39:32 +03:00
|
|
|
|
2018-03-25 00:18:59 +02:00
|
|
|
int av_packet_make_refcounted(AVPacket *pkt)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (pkt->buf)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ret = packet_alloc(&pkt->buf, pkt->size);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2019-02-14 23:36:46 +02:00
|
|
|
av_assert1(!pkt->size || pkt->data);
|
2018-03-25 00:18:59 +02:00
|
|
|
if (pkt->size)
|
|
|
|
memcpy(pkt->buf->data, pkt->data, pkt->size);
|
|
|
|
|
|
|
|
pkt->data = pkt->buf->data;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-03-19 17:39:08 +02:00
|
|
|
int av_packet_make_writable(AVPacket *pkt)
|
|
|
|
{
|
|
|
|
AVBufferRef *buf = NULL;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (pkt->buf && av_buffer_is_writable(pkt->buf))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
ret = packet_alloc(&buf, pkt->size);
|
|
|
|
if (ret < 0)
|
|
|
|
return ret;
|
2019-02-14 23:36:46 +02:00
|
|
|
av_assert1(!pkt->size || pkt->data);
|
2018-03-19 17:39:08 +02:00
|
|
|
if (pkt->size)
|
|
|
|
memcpy(buf->data, pkt->data, pkt->size);
|
|
|
|
|
|
|
|
av_buffer_unref(&pkt->buf);
|
|
|
|
pkt->buf = buf;
|
|
|
|
pkt->data = buf->data;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-06-18 10:39:32 +03:00
|
|
|
void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
|
|
|
|
{
|
|
|
|
if (pkt->pts != AV_NOPTS_VALUE)
|
|
|
|
pkt->pts = av_rescale_q(pkt->pts, src_tb, dst_tb);
|
|
|
|
if (pkt->dts != AV_NOPTS_VALUE)
|
|
|
|
pkt->dts = av_rescale_q(pkt->dts, src_tb, dst_tb);
|
|
|
|
if (pkt->duration > 0)
|
|
|
|
pkt->duration = av_rescale_q(pkt->duration, src_tb, dst_tb);
|
|
|
|
}
|
2015-07-21 13:23:29 +02:00
|
|
|
|
2021-12-16 02:49:39 +02:00
|
|
|
int avpriv_packet_list_put(PacketList *packet_buffer,
|
2020-08-17 17:03:50 +02:00
|
|
|
AVPacket *pkt,
|
|
|
|
int (*copy)(AVPacket *dst, const AVPacket *src),
|
|
|
|
int flags)
|
|
|
|
{
|
2021-12-16 02:49:39 +02:00
|
|
|
PacketListEntry *pktl = av_malloc(sizeof(*pktl));
|
2020-08-17 17:03:50 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (!pktl)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
if (copy) {
|
2021-05-07 04:55:32 +02:00
|
|
|
get_packet_defaults(&pktl->pkt);
|
2020-08-17 17:03:50 +02:00
|
|
|
ret = copy(&pktl->pkt, pkt);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_free(pktl);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ret = av_packet_make_refcounted(pkt);
|
|
|
|
if (ret < 0) {
|
|
|
|
av_free(pktl);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
av_packet_move_ref(&pktl->pkt, pkt);
|
|
|
|
}
|
|
|
|
|
2021-05-07 04:55:32 +02:00
|
|
|
pktl->next = NULL;
|
|
|
|
|
2021-12-16 02:49:39 +02:00
|
|
|
if (packet_buffer->head)
|
|
|
|
packet_buffer->tail->next = pktl;
|
2020-08-17 17:03:50 +02:00
|
|
|
else
|
2021-12-16 02:49:39 +02:00
|
|
|
packet_buffer->head = pktl;
|
2020-08-17 17:03:50 +02:00
|
|
|
|
|
|
|
/* Add the packet in the buffered packet list. */
|
2021-12-16 02:49:39 +02:00
|
|
|
packet_buffer->tail = pktl;
|
2020-08-17 17:03:50 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-12-16 02:49:39 +02:00
|
|
|
int avpriv_packet_list_get(PacketList *pkt_buffer,
|
2020-08-17 17:03:50 +02:00
|
|
|
AVPacket *pkt)
|
|
|
|
{
|
2021-12-16 02:49:39 +02:00
|
|
|
PacketListEntry *pktl = pkt_buffer->head;
|
|
|
|
if (!pktl)
|
2020-08-17 17:03:50 +02:00
|
|
|
return AVERROR(EAGAIN);
|
|
|
|
*pkt = pktl->pkt;
|
2021-12-16 02:49:39 +02:00
|
|
|
pkt_buffer->head = pktl->next;
|
|
|
|
if (!pkt_buffer->head)
|
|
|
|
pkt_buffer->tail = NULL;
|
2020-08-17 17:03:50 +02:00
|
|
|
av_freep(&pktl);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-12-16 02:49:39 +02:00
|
|
|
void avpriv_packet_list_free(PacketList *pkt_buf)
|
2020-08-17 17:03:50 +02:00
|
|
|
{
|
2021-12-16 02:49:39 +02:00
|
|
|
PacketListEntry *tmp = pkt_buf->head;
|
2020-08-17 17:03:50 +02:00
|
|
|
|
|
|
|
while (tmp) {
|
2021-12-16 02:49:39 +02:00
|
|
|
PacketListEntry *pktl = tmp;
|
2020-08-17 17:03:50 +02:00
|
|
|
tmp = pktl->next;
|
|
|
|
av_packet_unref(&pktl->pkt);
|
|
|
|
av_freep(&pktl);
|
|
|
|
}
|
2021-12-16 02:49:39 +02:00
|
|
|
pkt_buf->head = pkt_buf->tail = NULL;
|
2020-08-17 17:03:50 +02:00
|
|
|
}
|
|
|
|
|
2015-07-21 13:23:29 +02:00
|
|
|
int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
|
|
|
|
{
|
|
|
|
uint8_t *side_data;
|
2021-04-14 14:59:32 +02:00
|
|
|
size_t side_data_size;
|
2015-07-21 13:23:29 +02:00
|
|
|
int i;
|
|
|
|
|
|
|
|
side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, &side_data_size);
|
|
|
|
if (!side_data) {
|
|
|
|
side_data_size = 4+4+8*error_count;
|
|
|
|
side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
|
|
|
|
side_data_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!side_data || side_data_size < 4+4+8*error_count)
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
AV_WL32(side_data , quality );
|
|
|
|
side_data[4] = pict_type;
|
|
|
|
side_data[5] = error_count;
|
|
|
|
for (i = 0; i<error_count; i++)
|
|
|
|
AV_WL64(side_data+8 + 8*i , error[i]);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2019-10-04 16:54:28 +02:00
|
|
|
|
|
|
|
int ff_side_data_set_prft(AVPacket *pkt, int64_t timestamp)
|
|
|
|
{
|
|
|
|
AVProducerReferenceTime *prft;
|
|
|
|
uint8_t *side_data;
|
2021-04-14 14:59:32 +02:00
|
|
|
size_t side_data_size;
|
2019-10-04 16:54:28 +02:00
|
|
|
|
|
|
|
side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_PRFT, &side_data_size);
|
|
|
|
if (!side_data) {
|
|
|
|
side_data_size = sizeof(AVProducerReferenceTime);
|
|
|
|
side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_PRFT, side_data_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!side_data || side_data_size < sizeof(AVProducerReferenceTime))
|
|
|
|
return AVERROR(ENOMEM);
|
|
|
|
|
|
|
|
prft = (AVProducerReferenceTime *)side_data;
|
|
|
|
prft->wallclock = timestamp;
|
|
|
|
prft->flags = 0;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2023-07-16 20:03:49 +02:00
|
|
|
|
|
|
|
const AVPacketSideData *av_packet_side_data_get(const AVPacketSideData *sd, int nb_sd,
|
|
|
|
enum AVPacketSideDataType type)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < nb_sd; i++)
|
|
|
|
if (sd[i].type == type)
|
|
|
|
return &sd[i];
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static AVPacketSideData *packet_side_data_add(AVPacketSideData **psd, int *pnb_sd,
|
|
|
|
enum AVPacketSideDataType type,
|
|
|
|
void *data, size_t size)
|
|
|
|
{
|
|
|
|
AVPacketSideData *sd = *psd, *tmp;
|
|
|
|
int nb_sd = *pnb_sd;
|
|
|
|
|
|
|
|
for (int i = 0; i < nb_sd; i++) {
|
|
|
|
if (sd[i].type != type)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
av_free(sd[i].data);
|
|
|
|
sd[i].data = data;
|
|
|
|
sd[i].size = size;
|
|
|
|
return &sd[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nb_sd == INT_MAX)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
tmp = av_realloc_array(sd, nb_sd + 1, sizeof(*tmp));
|
|
|
|
if (!tmp)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
*psd = sd = tmp;
|
|
|
|
sd[nb_sd].type = type;
|
|
|
|
sd[nb_sd].data = data;
|
|
|
|
sd[nb_sd].size = size;
|
|
|
|
*pnb_sd = nb_sd + 1;
|
|
|
|
|
|
|
|
return &sd[nb_sd];
|
|
|
|
}
|
|
|
|
|
|
|
|
AVPacketSideData *av_packet_side_data_add(AVPacketSideData **psd, int *pnb_sd,
|
|
|
|
enum AVPacketSideDataType type,
|
|
|
|
void *data, size_t size, int flags)
|
|
|
|
{
|
|
|
|
return packet_side_data_add(psd, pnb_sd, type, data, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
AVPacketSideData *av_packet_side_data_new(AVPacketSideData **psd, int *pnb_sd,
|
|
|
|
enum AVPacketSideDataType type,
|
|
|
|
size_t size, int flags)
|
|
|
|
{
|
|
|
|
AVPacketSideData *sd = NULL;
|
|
|
|
uint8_t *data;
|
|
|
|
|
|
|
|
if (size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
data = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
|
|
|
|
if (!data)
|
|
|
|
return NULL;
|
|
|
|
memset(data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
|
|
|
|
|
|
|
|
sd = packet_side_data_add(psd, pnb_sd, type, data, size);
|
|
|
|
if (!sd)
|
|
|
|
av_freep(&data);
|
|
|
|
|
|
|
|
return sd;
|
|
|
|
}
|
|
|
|
|
|
|
|
void av_packet_side_data_remove(AVPacketSideData *sd, int *pnb_sd,
|
|
|
|
enum AVPacketSideDataType type)
|
|
|
|
{
|
|
|
|
int nb_sd = *pnb_sd;
|
|
|
|
|
|
|
|
for (int i = nb_sd - 1; i >= 0; i--) {
|
|
|
|
if (sd[i].type != type)
|
|
|
|
continue;
|
|
|
|
av_free(sd[i].data);
|
|
|
|
sd[i] = sd[--nb_sd];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
*pnb_sd = nb_sd;
|
|
|
|
}
|
|
|
|
|
|
|
|
void av_packet_side_data_free(AVPacketSideData **psd, int *pnb_sd)
|
|
|
|
{
|
|
|
|
AVPacketSideData *sd = *psd;
|
|
|
|
int nb_sd = *pnb_sd;
|
|
|
|
|
|
|
|
for (int i = 0; i < nb_sd; i++)
|
|
|
|
av_free(sd[i].data);
|
|
|
|
|
|
|
|
av_freep(psd);
|
|
|
|
*pnb_sd = 0;
|
|
|
|
}
|