2 * AVPacket functions for libavcodec
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libavutil/avassert.h"
25 #include "libavutil/intreadwrite.h"
26 #include "libavutil/mathematics.h"
27 #include "libavutil/mem.h"
28 #include "libavutil/rational.h"
32 #include "packet_internal.h"
34 #if FF_API_INIT_PACKET
35 void av_init_packet(AVPacket
*pkt
)
37 pkt
->pts
= AV_NOPTS_VALUE
;
38 pkt
->dts
= AV_NOPTS_VALUE
;
42 pkt
->stream_index
= 0;
44 pkt
->side_data
= NULL
;
45 pkt
->side_data_elems
= 0;
47 pkt
->opaque_ref
= NULL
;
48 pkt
->time_base
= av_make_q(0, 1);
52 static void get_packet_defaults(AVPacket
*pkt
)
54 memset(pkt
, 0, sizeof(*pkt
));
56 pkt
->pts
= AV_NOPTS_VALUE
;
57 pkt
->dts
= AV_NOPTS_VALUE
;
59 pkt
->time_base
= av_make_q(0, 1);
62 AVPacket
*av_packet_alloc(void)
64 AVPacket
*pkt
= av_malloc(sizeof(AVPacket
));
68 get_packet_defaults(pkt
);
73 void av_packet_free(AVPacket
**pkt
)
78 av_packet_unref(*pkt
);
82 static int packet_alloc(AVBufferRef
**buf
, int size
)
85 if (size
< 0 || size
>= INT_MAX
- AV_INPUT_BUFFER_PADDING_SIZE
)
86 return AVERROR(EINVAL
);
88 ret
= av_buffer_realloc(buf
, size
+ AV_INPUT_BUFFER_PADDING_SIZE
);
92 memset((*buf
)->data
+ size
, 0, AV_INPUT_BUFFER_PADDING_SIZE
);
97 int av_new_packet(AVPacket
*pkt
, int size
)
99 AVBufferRef
*buf
= NULL
;
100 int ret
= packet_alloc(&buf
, size
);
104 get_packet_defaults(pkt
);
106 pkt
->data
= buf
->data
;
112 void av_shrink_packet(AVPacket
*pkt
, int size
)
114 if (pkt
->size
<= size
)
117 memset(pkt
->data
+ size
, 0, AV_INPUT_BUFFER_PADDING_SIZE
);
120 int av_grow_packet(AVPacket
*pkt
, int grow_by
)
123 av_assert0((unsigned)pkt
->size
<= INT_MAX
- AV_INPUT_BUFFER_PADDING_SIZE
);
124 if ((unsigned)grow_by
>
125 INT_MAX
- (pkt
->size
+ AV_INPUT_BUFFER_PADDING_SIZE
))
126 return AVERROR(ENOMEM
);
128 new_size
= pkt
->size
+ grow_by
+ AV_INPUT_BUFFER_PADDING_SIZE
;
131 uint8_t *old_data
= pkt
->data
;
132 if (pkt
->data
== NULL
) {
134 pkt
->data
= pkt
->buf
->data
;
136 data_offset
= pkt
->data
- pkt
->buf
->data
;
137 if (data_offset
> INT_MAX
- new_size
)
138 return AVERROR(ENOMEM
);
141 if (new_size
+ data_offset
> pkt
->buf
->size
||
142 !av_buffer_is_writable(pkt
->buf
)) {
145 // allocate slightly more than requested to avoid excessive
147 if (new_size
+ data_offset
< INT_MAX
- new_size
/16)
148 new_size
+= new_size
/16;
150 ret
= av_buffer_realloc(&pkt
->buf
, new_size
+ data_offset
);
152 pkt
->data
= old_data
;
155 pkt
->data
= pkt
->buf
->data
+ data_offset
;
158 pkt
->buf
= av_buffer_alloc(new_size
);
160 return AVERROR(ENOMEM
);
162 memcpy(pkt
->buf
->data
, pkt
->data
, pkt
->size
);
163 pkt
->data
= pkt
->buf
->data
;
165 pkt
->size
+= grow_by
;
166 memset(pkt
->data
+ pkt
->size
, 0, AV_INPUT_BUFFER_PADDING_SIZE
);
171 int av_packet_from_data(AVPacket
*pkt
, uint8_t *data
, int size
)
173 if (size
>= INT_MAX
- AV_INPUT_BUFFER_PADDING_SIZE
)
174 return AVERROR(EINVAL
);
176 pkt
->buf
= av_buffer_create(data
, size
+ AV_INPUT_BUFFER_PADDING_SIZE
,
177 av_buffer_default_free
, NULL
, 0);
179 return AVERROR(ENOMEM
);
187 void av_packet_free_side_data(AVPacket
*pkt
)
190 for (i
= 0; i
< pkt
->side_data_elems
; i
++)
191 av_freep(&pkt
->side_data
[i
].data
);
192 av_freep(&pkt
->side_data
);
193 pkt
->side_data_elems
= 0;
196 int av_packet_add_side_data(AVPacket
*pkt
, enum AVPacketSideDataType type
,
197 uint8_t *data
, size_t size
)
199 AVPacketSideData
*tmp
;
200 int i
, elems
= pkt
->side_data_elems
;
202 for (i
= 0; i
< elems
; i
++) {
203 AVPacketSideData
*sd
= &pkt
->side_data
[i
];
205 if (sd
->type
== type
) {
213 if ((unsigned)elems
+ 1 > AV_PKT_DATA_NB
)
214 return AVERROR(ERANGE
);
216 tmp
= av_realloc(pkt
->side_data
, (elems
+ 1) * sizeof(*tmp
));
218 return AVERROR(ENOMEM
);
220 pkt
->side_data
= tmp
;
221 pkt
->side_data
[elems
].data
= data
;
222 pkt
->side_data
[elems
].size
= size
;
223 pkt
->side_data
[elems
].type
= type
;
224 pkt
->side_data_elems
++;
230 uint8_t *av_packet_new_side_data(AVPacket
*pkt
, enum AVPacketSideDataType type
,
236 if (size
> SIZE_MAX
- AV_INPUT_BUFFER_PADDING_SIZE
)
238 data
= av_mallocz(size
+ AV_INPUT_BUFFER_PADDING_SIZE
);
242 ret
= av_packet_add_side_data(pkt
, type
, data
, size
);
251 uint8_t *av_packet_get_side_data(const AVPacket
*pkt
, enum AVPacketSideDataType type
,
256 for (i
= 0; i
< pkt
->side_data_elems
; i
++) {
257 if (pkt
->side_data
[i
].type
== type
) {
259 *size
= pkt
->side_data
[i
].size
;
260 return pkt
->side_data
[i
].data
;
268 const char *av_packet_side_data_name(enum AVPacketSideDataType type
)
271 case AV_PKT_DATA_PALETTE
: return "Palette";
272 case AV_PKT_DATA_NEW_EXTRADATA
: return "New Extradata";
273 case AV_PKT_DATA_PARAM_CHANGE
: return "Param Change";
274 case AV_PKT_DATA_H263_MB_INFO
: return "H263 MB Info";
275 case AV_PKT_DATA_REPLAYGAIN
: return "Replay Gain";
276 case AV_PKT_DATA_DISPLAYMATRIX
: return "Display Matrix";
277 case AV_PKT_DATA_STEREO3D
: return "Stereo 3D";
278 case AV_PKT_DATA_AUDIO_SERVICE_TYPE
: return "Audio Service Type";
279 case AV_PKT_DATA_QUALITY_STATS
: return "Quality stats";
280 case AV_PKT_DATA_FALLBACK_TRACK
: return "Fallback track";
281 case AV_PKT_DATA_CPB_PROPERTIES
: return "CPB properties";
282 case AV_PKT_DATA_SKIP_SAMPLES
: return "Skip Samples";
283 case AV_PKT_DATA_JP_DUALMONO
: return "JP Dual Mono";
284 case AV_PKT_DATA_STRINGS_METADATA
: return "Strings Metadata";
285 case AV_PKT_DATA_SUBTITLE_POSITION
: return "Subtitle Position";
286 case AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL
: return "Matroska BlockAdditional";
287 case AV_PKT_DATA_WEBVTT_IDENTIFIER
: return "WebVTT ID";
288 case AV_PKT_DATA_WEBVTT_SETTINGS
: return "WebVTT Settings";
289 case AV_PKT_DATA_METADATA_UPDATE
: return "Metadata Update";
290 case AV_PKT_DATA_MPEGTS_STREAM_ID
: return "MPEGTS Stream ID";
291 case AV_PKT_DATA_MASTERING_DISPLAY_METADATA
: return "Mastering display metadata";
292 case AV_PKT_DATA_CONTENT_LIGHT_LEVEL
: return "Content light level metadata";
293 case AV_PKT_DATA_SPHERICAL
: return "Spherical Mapping";
294 case AV_PKT_DATA_A53_CC
: return "A53 Closed Captions";
295 case AV_PKT_DATA_ENCRYPTION_INIT_INFO
: return "Encryption initialization data";
296 case AV_PKT_DATA_ENCRYPTION_INFO
: return "Encryption info";
297 case AV_PKT_DATA_AFD
: return "Active Format Description data";
298 case AV_PKT_DATA_PRFT
: return "Producer Reference Time";
299 case AV_PKT_DATA_ICC_PROFILE
: return "ICC Profile";
300 case AV_PKT_DATA_DOVI_CONF
: return "DOVI configuration record";
301 case AV_PKT_DATA_S12M_TIMECODE
: return "SMPTE ST 12-1:2014 timecode";
302 case AV_PKT_DATA_DYNAMIC_HDR10_PLUS
: return "HDR10+ Dynamic Metadata (SMPTE 2094-40)";
307 uint8_t *av_packet_pack_dictionary(AVDictionary
*dict
, size_t *size
)
309 uint8_t *data
= NULL
;
315 for (int pass
= 0; pass
< 2; pass
++) {
316 const AVDictionaryEntry
*t
= NULL
;
317 size_t total_length
= 0;
319 while ((t
= av_dict_get(dict
, "", t
, AV_DICT_IGNORE_SUFFIX
))) {
320 for (int i
= 0; i
< 2; i
++) {
321 const char *str
= i
? t
->value
: t
->key
;
322 const size_t len
= strlen(str
) + 1;
325 memcpy(data
+ total_length
, str
, len
);
326 else if (len
> SIZE_MAX
- total_length
)
333 data
= av_malloc(total_length
);
336 *size
= total_length
;
342 int av_packet_unpack_dictionary(const uint8_t *data
, size_t size
,
348 if (!dict
|| !data
|| !size
)
352 return AVERROR_INVALIDDATA
;
354 const uint8_t *key
= data
;
355 const uint8_t *val
= data
+ strlen(key
) + 1;
357 if (val
>= end
|| !*key
)
358 return AVERROR_INVALIDDATA
;
360 ret
= av_dict_set(dict
, key
, val
, 0);
363 data
= val
+ strlen(val
) + 1;
369 int av_packet_shrink_side_data(AVPacket
*pkt
, enum AVPacketSideDataType type
,
374 for (i
= 0; i
< pkt
->side_data_elems
; i
++) {
375 if (pkt
->side_data
[i
].type
== type
) {
376 if (size
> pkt
->side_data
[i
].size
)
377 return AVERROR(ENOMEM
);
378 pkt
->side_data
[i
].size
= size
;
382 return AVERROR(ENOENT
);
385 int av_packet_copy_props(AVPacket
*dst
, const AVPacket
*src
)
392 dst
->duration
= src
->duration
;
393 dst
->flags
= src
->flags
;
394 dst
->stream_index
= src
->stream_index
;
395 dst
->opaque
= src
->opaque
;
396 dst
->time_base
= src
->time_base
;
397 dst
->opaque_ref
= NULL
;
398 dst
->side_data
= NULL
;
399 dst
->side_data_elems
= 0;
401 ret
= av_buffer_replace(&dst
->opaque_ref
, src
->opaque_ref
);
405 for (i
= 0; i
< src
->side_data_elems
; i
++) {
406 enum AVPacketSideDataType type
= src
->side_data
[i
].type
;
407 size_t size
= src
->side_data
[i
].size
;
408 uint8_t *src_data
= src
->side_data
[i
].data
;
409 uint8_t *dst_data
= av_packet_new_side_data(dst
, type
, size
);
412 av_buffer_unref(&dst
->opaque_ref
);
413 av_packet_free_side_data(dst
);
414 return AVERROR(ENOMEM
);
416 memcpy(dst_data
, src_data
, size
);
422 void av_packet_unref(AVPacket
*pkt
)
424 av_packet_free_side_data(pkt
);
425 av_buffer_unref(&pkt
->opaque_ref
);
426 av_buffer_unref(&pkt
->buf
);
427 get_packet_defaults(pkt
);
430 int av_packet_ref(AVPacket
*dst
, const AVPacket
*src
)
436 ret
= av_packet_copy_props(dst
, src
);
441 ret
= packet_alloc(&dst
->buf
, src
->size
);
444 av_assert1(!src
->size
|| src
->data
);
446 memcpy(dst
->buf
->data
, src
->data
, src
->size
);
448 dst
->data
= dst
->buf
->data
;
450 dst
->buf
= av_buffer_ref(src
->buf
);
452 ret
= AVERROR(ENOMEM
);
455 dst
->data
= src
->data
;
458 dst
->size
= src
->size
;
462 av_packet_unref(dst
);
466 AVPacket
*av_packet_clone(const AVPacket
*src
)
468 AVPacket
*ret
= av_packet_alloc();
473 if (av_packet_ref(ret
, src
))
474 av_packet_free(&ret
);
479 void av_packet_move_ref(AVPacket
*dst
, AVPacket
*src
)
482 get_packet_defaults(src
);
485 int av_packet_make_refcounted(AVPacket
*pkt
)
492 ret
= packet_alloc(&pkt
->buf
, pkt
->size
);
495 av_assert1(!pkt
->size
|| pkt
->data
);
497 memcpy(pkt
->buf
->data
, pkt
->data
, pkt
->size
);
499 pkt
->data
= pkt
->buf
->data
;
504 int av_packet_make_writable(AVPacket
*pkt
)
506 AVBufferRef
*buf
= NULL
;
509 if (pkt
->buf
&& av_buffer_is_writable(pkt
->buf
))
512 ret
= packet_alloc(&buf
, pkt
->size
);
515 av_assert1(!pkt
->size
|| pkt
->data
);
517 memcpy(buf
->data
, pkt
->data
, pkt
->size
);
519 av_buffer_unref(&pkt
->buf
);
521 pkt
->data
= buf
->data
;
526 void av_packet_rescale_ts(AVPacket
*pkt
, AVRational src_tb
, AVRational dst_tb
)
528 if (pkt
->pts
!= AV_NOPTS_VALUE
)
529 pkt
->pts
= av_rescale_q(pkt
->pts
, src_tb
, dst_tb
);
530 if (pkt
->dts
!= AV_NOPTS_VALUE
)
531 pkt
->dts
= av_rescale_q(pkt
->dts
, src_tb
, dst_tb
);
532 if (pkt
->duration
> 0)
533 pkt
->duration
= av_rescale_q(pkt
->duration
, src_tb
, dst_tb
);
536 int avpriv_packet_list_put(PacketList
*packet_buffer
,
538 int (*copy
)(AVPacket
*dst
, const AVPacket
*src
),
541 PacketListEntry
*pktl
= av_malloc(sizeof(*pktl
));
545 return AVERROR(ENOMEM
);
548 get_packet_defaults(&pktl
->pkt
);
549 ret
= copy(&pktl
->pkt
, pkt
);
555 ret
= av_packet_make_refcounted(pkt
);
560 av_packet_move_ref(&pktl
->pkt
, pkt
);
565 if (packet_buffer
->head
)
566 packet_buffer
->tail
->next
= pktl
;
568 packet_buffer
->head
= pktl
;
570 /* Add the packet in the buffered packet list. */
571 packet_buffer
->tail
= pktl
;
575 int avpriv_packet_list_get(PacketList
*pkt_buffer
,
578 PacketListEntry
*pktl
= pkt_buffer
->head
;
580 return AVERROR(EAGAIN
);
582 pkt_buffer
->head
= pktl
->next
;
583 if (!pkt_buffer
->head
)
584 pkt_buffer
->tail
= NULL
;
589 void avpriv_packet_list_free(PacketList
*pkt_buf
)
591 PacketListEntry
*tmp
= pkt_buf
->head
;
594 PacketListEntry
*pktl
= tmp
;
596 av_packet_unref(&pktl
->pkt
);
599 pkt_buf
->head
= pkt_buf
->tail
= NULL
;
602 int ff_side_data_set_encoder_stats(AVPacket
*pkt
, int quality
, int64_t *error
, int error_count
, int pict_type
)
605 size_t side_data_size
;
608 side_data
= av_packet_get_side_data(pkt
, AV_PKT_DATA_QUALITY_STATS
, &side_data_size
);
610 side_data_size
= 4+4+8*error_count
;
611 side_data
= av_packet_new_side_data(pkt
, AV_PKT_DATA_QUALITY_STATS
,
615 if (!side_data
|| side_data_size
< 4+4+8*error_count
)
616 return AVERROR(ENOMEM
);
618 AV_WL32(side_data
, quality
);
619 side_data
[4] = pict_type
;
620 side_data
[5] = error_count
;
621 for (i
= 0; i
<error_count
; i
++)
622 AV_WL64(side_data
+8 + 8*i
, error
[i
]);
627 int ff_side_data_set_prft(AVPacket
*pkt
, int64_t timestamp
)
629 AVProducerReferenceTime
*prft
;
631 size_t side_data_size
;
633 side_data
= av_packet_get_side_data(pkt
, AV_PKT_DATA_PRFT
, &side_data_size
);
635 side_data_size
= sizeof(AVProducerReferenceTime
);
636 side_data
= av_packet_new_side_data(pkt
, AV_PKT_DATA_PRFT
, side_data_size
);
639 if (!side_data
|| side_data_size
< sizeof(AVProducerReferenceTime
))
640 return AVERROR(ENOMEM
);
642 prft
= (AVProducerReferenceTime
*)side_data
;
643 prft
->wallclock
= timestamp
;