GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/avpacket.c Lines: 281 485 57.9 %
Date: 2021-04-20 04:37:23 Branches: 126 286 44.1 %

Line Branch Exec Source
1
/*
2
 * AVPacket functions for libavcodec
3
 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4
 *
5
 * This file is part of FFmpeg.
6
 *
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.
11
 *
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.
16
 *
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
20
 */
21
22
#include <string.h>
23
24
#include "libavutil/avassert.h"
25
#include "libavutil/common.h"
26
#include "libavutil/internal.h"
27
#include "libavutil/mathematics.h"
28
#include "libavutil/mem.h"
29
30
#include "bytestream.h"
31
#include "internal.h"
32
#include "packet.h"
33
#include "packet_internal.h"
34
35
#if FF_API_INIT_PACKET
36
1242037
void av_init_packet(AVPacket *pkt)
37
{
38
1242037
    pkt->pts                  = AV_NOPTS_VALUE;
39
1242037
    pkt->dts                  = AV_NOPTS_VALUE;
40
1242037
    pkt->pos                  = -1;
41
1242037
    pkt->duration             = 0;
42
#if FF_API_CONVERGENCE_DURATION
43
FF_DISABLE_DEPRECATION_WARNINGS
44
1242037
    pkt->convergence_duration = 0;
45
FF_ENABLE_DEPRECATION_WARNINGS
46
#endif
47
1242037
    pkt->flags                = 0;
48
1242037
    pkt->stream_index         = 0;
49
1242037
    pkt->buf                  = NULL;
50
1242037
    pkt->side_data            = NULL;
51
1242037
    pkt->side_data_elems      = 0;
52
1242037
}
53
#endif
54
55
9951241
static void get_packet_defaults(AVPacket *pkt)
56
{
57
9951241
    memset(pkt, 0, sizeof(*pkt));
58
59
9951241
    pkt->pts             = AV_NOPTS_VALUE;
60
9951241
    pkt->dts             = AV_NOPTS_VALUE;
61
9951241
    pkt->pos             = -1;
62
9951241
}
63
64
142586
AVPacket *av_packet_alloc(void)
65
{
66
142586
    AVPacket *pkt = av_mallocz(sizeof(AVPacket));
67
142586
    if (!pkt)
68
        return pkt;
69
70
142586
    get_packet_defaults(pkt);
71
72
142586
    return pkt;
73
}
74
75
143530
void av_packet_free(AVPacket **pkt)
76
{
77

143530
    if (!pkt || !*pkt)
78
944
        return;
79
80
142586
    av_packet_unref(*pkt);
81
142586
    av_freep(pkt);
82
}
83
84
1129550
static int packet_alloc(AVBufferRef **buf, int size)
85
{
86
    int ret;
87

1129550
    if (size < 0 || size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
88
1
        return AVERROR(EINVAL);
89
90
1129549
    ret = av_buffer_realloc(buf, size + AV_INPUT_BUFFER_PADDING_SIZE);
91
1129549
    if (ret < 0)
92
        return ret;
93
94
1129549
    memset((*buf)->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
95
96
1129549
    return 0;
97
}
98
99
961198
int av_new_packet(AVPacket *pkt, int size)
100
{
101
961198
    AVBufferRef *buf = NULL;
102
961198
    int ret = packet_alloc(&buf, size);
103
961198
    if (ret < 0)
104
1
        return ret;
105
106
961197
    get_packet_defaults(pkt);
107
961197
    pkt->buf      = buf;
108
961197
    pkt->data     = buf->data;
109
961197
    pkt->size     = size;
110
111
961197
    return 0;
112
}
113
114
500516
void av_shrink_packet(AVPacket *pkt, int size)
115
{
116
500516
    if (pkt->size <= size)
117
497468
        return;
118
3048
    pkt->size = size;
119
3048
    memset(pkt->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
120
}
121
122
283735
int av_grow_packet(AVPacket *pkt, int grow_by)
123
{
124
    int new_size;
125
283735
    av_assert0((unsigned)pkt->size <= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
126
283735
    if ((unsigned)grow_by >
127
283735
        INT_MAX - (pkt->size + AV_INPUT_BUFFER_PADDING_SIZE))
128
1
        return AVERROR(ENOMEM);
129
130
283734
    new_size = pkt->size + grow_by + AV_INPUT_BUFFER_PADDING_SIZE;
131
283734
    if (pkt->buf) {
132
        size_t data_offset;
133
13292
        uint8_t *old_data = pkt->data;
134
13292
        if (pkt->data == NULL) {
135
            data_offset = 0;
136
            pkt->data = pkt->buf->data;
137
        } else {
138
13292
            data_offset = pkt->data - pkt->buf->data;
139
13292
            if (data_offset > INT_MAX - new_size)
140
                return AVERROR(ENOMEM);
141
        }
142
143

13293
        if (new_size + data_offset > pkt->buf->size ||
144
1
            !av_buffer_is_writable(pkt->buf)) {
145
13291
            int ret = av_buffer_realloc(&pkt->buf, new_size + data_offset);
146
13291
            if (ret < 0) {
147
                pkt->data = old_data;
148
                return ret;
149
            }
150
13291
            pkt->data = pkt->buf->data + data_offset;
151
        }
152
    } else {
153
270442
        pkt->buf = av_buffer_alloc(new_size);
154
270442
        if (!pkt->buf)
155
            return AVERROR(ENOMEM);
156
270442
        if (pkt->size > 0)
157
            memcpy(pkt->buf->data, pkt->data, pkt->size);
158
270442
        pkt->data = pkt->buf->data;
159
    }
160
283734
    pkt->size += grow_by;
161
283734
    memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
162
163
283734
    return 0;
164
}
165
166
1
int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
167
{
168
1
    if (size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
169
1
        return AVERROR(EINVAL);
170
171
    pkt->buf = av_buffer_create(data, size + AV_INPUT_BUFFER_PADDING_SIZE,
172
                                av_buffer_default_free, NULL, 0);
173
    if (!pkt->buf)
174
        return AVERROR(ENOMEM);
175
176
    pkt->data = data;
177
    pkt->size = size;
178
179
    return 0;
180
}
181
182
#if FF_API_AVPACKET_OLD_API
183
FF_DISABLE_DEPRECATION_WARNINGS
184
#define ALLOC_MALLOC(data, size) data = av_malloc(size)
185
#define ALLOC_BUF(data, size)                \
186
do {                                         \
187
    av_buffer_realloc(&pkt->buf, size);      \
188
    data = pkt->buf ? pkt->buf->data : NULL; \
189
} while (0)
190
191
#define DUP_DATA(dst, src, size, padding, ALLOC)                        \
192
    do {                                                                \
193
        void *data;                                                     \
194
        if (padding) {                                                  \
195
            if ((unsigned)(size) >                                      \
196
                (unsigned)(size) + AV_INPUT_BUFFER_PADDING_SIZE)        \
197
                goto failed_alloc;                                      \
198
            ALLOC(data, size + AV_INPUT_BUFFER_PADDING_SIZE);           \
199
        } else {                                                        \
200
            ALLOC(data, size);                                          \
201
        }                                                               \
202
        if (!data)                                                      \
203
            goto failed_alloc;                                          \
204
        memcpy(data, src, size);                                        \
205
        if (padding)                                                    \
206
            memset((uint8_t *)data + size, 0,                           \
207
                   AV_INPUT_BUFFER_PADDING_SIZE);                       \
208
        dst = data;                                                     \
209
    } while (0)
210
211
/* Makes duplicates of data, side_data, but does not copy any other fields */
212
static int copy_packet_data(AVPacket *pkt, const AVPacket *src, int dup)
213
{
214
    pkt->data      = NULL;
215
    pkt->side_data = NULL;
216
    pkt->side_data_elems = 0;
217
    if (pkt->buf) {
218
        AVBufferRef *ref = av_buffer_ref(src->buf);
219
        if (!ref)
220
            return AVERROR(ENOMEM);
221
        pkt->buf  = ref;
222
        pkt->data = ref->data;
223
    } else {
224
        DUP_DATA(pkt->data, src->data, pkt->size, 1, ALLOC_BUF);
225
    }
226
    if (src->side_data_elems && dup) {
227
        pkt->side_data = src->side_data;
228
        pkt->side_data_elems = src->side_data_elems;
229
    }
230
    if (src->side_data_elems && !dup) {
231
        return av_copy_packet_side_data(pkt, src);
232
    }
233
    return 0;
234
235
failed_alloc:
236
    av_packet_unref(pkt);
237
    return AVERROR(ENOMEM);
238
}
239
240
int av_copy_packet_side_data(AVPacket *pkt, const AVPacket *src)
241
{
242
    if (src->side_data_elems) {
243
        int i;
244
        DUP_DATA(pkt->side_data, src->side_data,
245
                src->side_data_elems * sizeof(*src->side_data), 0, ALLOC_MALLOC);
246
        if (src != pkt) {
247
            memset(pkt->side_data, 0,
248
                   src->side_data_elems * sizeof(*src->side_data));
249
        }
250
        for (i = 0; i < src->side_data_elems; i++) {
251
            DUP_DATA(pkt->side_data[i].data, src->side_data[i].data,
252
                    src->side_data[i].size, 1, ALLOC_MALLOC);
253
            pkt->side_data[i].size = src->side_data[i].size;
254
            pkt->side_data[i].type = src->side_data[i].type;
255
        }
256
    }
257
    pkt->side_data_elems = src->side_data_elems;
258
    return 0;
259
260
failed_alloc:
261
    av_packet_unref(pkt);
262
    return AVERROR(ENOMEM);
263
}
264
265
int av_dup_packet(AVPacket *pkt)
266
{
267
    AVPacket tmp_pkt;
268
269
    if (!pkt->buf && pkt->data) {
270
        tmp_pkt = *pkt;
271
        return copy_packet_data(pkt, &tmp_pkt, 1);
272
    }
273
    return 0;
274
}
275
276
int av_copy_packet(AVPacket *dst, const AVPacket *src)
277
{
278
    *dst = *src;
279
    return copy_packet_data(dst, src, 0);
280
}
281
FF_ENABLE_DEPRECATION_WARNINGS
282
#endif
283
284
6889920
void av_packet_free_side_data(AVPacket *pkt)
285
{
286
    int i;
287
6942942
    for (i = 0; i < pkt->side_data_elems; i++)
288
53022
        av_freep(&pkt->side_data[i].data);
289
6889920
    av_freep(&pkt->side_data);
290
6889920
    pkt->side_data_elems = 0;
291
6889920
}
292
293
#if FF_API_AVPACKET_OLD_API
294
FF_DISABLE_DEPRECATION_WARNINGS
295
void av_free_packet(AVPacket *pkt)
296
{
297
    if (pkt) {
298
        if (pkt->buf)
299
            av_buffer_unref(&pkt->buf);
300
        pkt->data            = NULL;
301
        pkt->size            = 0;
302
303
        av_packet_free_side_data(pkt);
304
    }
305
}
306
FF_ENABLE_DEPRECATION_WARNINGS
307
#endif
308
309
53022
int av_packet_add_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
310
                            uint8_t *data, size_t size)
311
{
312
    AVPacketSideData *tmp;
313
53022
    int i, elems = pkt->side_data_elems;
314
315
53165
    for (i = 0; i < elems; i++) {
316
143
        AVPacketSideData *sd = &pkt->side_data[i];
317
318
143
        if (sd->type == type) {
319
            av_free(sd->data);
320
            sd->data = data;
321
            sd->size = size;
322
            return 0;
323
        }
324
    }
325
326
53022
    if ((unsigned)elems + 1 > AV_PKT_DATA_NB)
327
        return AVERROR(ERANGE);
328
329
53022
    tmp = av_realloc(pkt->side_data, (elems + 1) * sizeof(*tmp));
330
53022
    if (!tmp)
331
        return AVERROR(ENOMEM);
332
333
53022
    pkt->side_data = tmp;
334
53022
    pkt->side_data[elems].data = data;
335
53022
    pkt->side_data[elems].size = size;
336
53022
    pkt->side_data[elems].type = type;
337
53022
    pkt->side_data_elems++;
338
339
53022
    return 0;
340
}
341
342
343
51734
uint8_t *av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
344
                                 buffer_size_t size)
345
{
346
    int ret;
347
    uint8_t *data;
348
349
#if FF_API_BUFFER_SIZE_T
350
51734
    if ((unsigned)size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
351
#else
352
    if (size > SIZE_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
353
#endif
354
        return NULL;
355
51734
    data = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
356
51734
    if (!data)
357
        return NULL;
358
359
51734
    ret = av_packet_add_side_data(pkt, type, data, size);
360
51734
    if (ret < 0) {
361
        av_freep(&data);
362
        return NULL;
363
    }
364
365
51734
    return data;
366
}
367
368
5430967
uint8_t *av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type,
369
                                 buffer_size_t *size)
370
{
371
    int i;
372
373
5502648
    for (i = 0; i < pkt->side_data_elems; i++) {
374
82075
        if (pkt->side_data[i].type == type) {
375
10394
            if (size)
376
1055
                *size = pkt->side_data[i].size;
377
10394
            return pkt->side_data[i].data;
378
        }
379
    }
380
5420573
    if (size)
381
5292313
        *size = 0;
382
5420573
    return NULL;
383
}
384
385
220
const char *av_packet_side_data_name(enum AVPacketSideDataType type)
386
{
387








220
    switch(type) {
388
    case AV_PKT_DATA_PALETTE:                    return "Palette";
389
1
    case AV_PKT_DATA_NEW_EXTRADATA:              return "New Extradata";
390
    case AV_PKT_DATA_PARAM_CHANGE:               return "Param Change";
391
    case AV_PKT_DATA_H263_MB_INFO:               return "H263 MB Info";
392
    case AV_PKT_DATA_REPLAYGAIN:                 return "Replay Gain";
393
2
    case AV_PKT_DATA_DISPLAYMATRIX:              return "Display Matrix";
394
5
    case AV_PKT_DATA_STEREO3D:                   return "Stereo 3D";
395
1
    case AV_PKT_DATA_AUDIO_SERVICE_TYPE:         return "Audio Service Type";
396
    case AV_PKT_DATA_QUALITY_STATS:              return "Quality stats";
397
    case AV_PKT_DATA_FALLBACK_TRACK:             return "Fallback track";
398
1
    case AV_PKT_DATA_CPB_PROPERTIES:             return "CPB properties";
399
5
    case AV_PKT_DATA_SKIP_SAMPLES:               return "Skip Samples";
400
    case AV_PKT_DATA_JP_DUALMONO:                return "JP Dual Mono";
401
131
    case AV_PKT_DATA_STRINGS_METADATA:           return "Strings Metadata";
402
    case AV_PKT_DATA_SUBTITLE_POSITION:          return "Subtitle Position";
403
    case AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL:   return "Matroska BlockAdditional";
404
    case AV_PKT_DATA_WEBVTT_IDENTIFIER:          return "WebVTT ID";
405
    case AV_PKT_DATA_WEBVTT_SETTINGS:            return "WebVTT Settings";
406
    case AV_PKT_DATA_METADATA_UPDATE:            return "Metadata Update";
407
64
    case AV_PKT_DATA_MPEGTS_STREAM_ID:           return "MPEGTS Stream ID";
408
3
    case AV_PKT_DATA_MASTERING_DISPLAY_METADATA: return "Mastering display metadata";
409
3
    case AV_PKT_DATA_CONTENT_LIGHT_LEVEL:        return "Content light level metadata";
410
4
    case AV_PKT_DATA_SPHERICAL:                  return "Spherical Mapping";
411
    case AV_PKT_DATA_A53_CC:                     return "A53 Closed Captions";
412
    case AV_PKT_DATA_ENCRYPTION_INIT_INFO:       return "Encryption initialization data";
413
    case AV_PKT_DATA_ENCRYPTION_INFO:            return "Encryption info";
414
    case AV_PKT_DATA_AFD:                        return "Active Format Description data";
415
    case AV_PKT_DATA_PRFT:                       return "Producer Reference Time";
416
    case AV_PKT_DATA_ICC_PROFILE:                return "ICC Profile";
417
    case AV_PKT_DATA_DOVI_CONF:                  return "DOVI configuration record";
418
    case AV_PKT_DATA_S12M_TIMECODE:              return "SMPTE ST 12-1:2014 timecode";
419
    }
420
    return NULL;
421
}
422
423
#if FF_API_MERGE_SD_API
424
425
#define FF_MERGE_MARKER 0x8c4d9d108e25e9feULL
426
427
int av_packet_merge_side_data(AVPacket *pkt){
428
    if(pkt->side_data_elems){
429
        AVBufferRef *buf;
430
        int i;
431
        uint8_t *p;
432
        uint64_t size= pkt->size + 8LL + AV_INPUT_BUFFER_PADDING_SIZE;
433
        AVPacket old= *pkt;
434
        for (i=0; i<old.side_data_elems; i++) {
435
            size += old.side_data[i].size + 5LL;
436
        }
437
        if (size > INT_MAX)
438
            return AVERROR(EINVAL);
439
        buf = av_buffer_alloc(size);
440
        if (!buf)
441
            return AVERROR(ENOMEM);
442
        pkt->buf = buf;
443
        pkt->data = p = buf->data;
444
        pkt->size = size - AV_INPUT_BUFFER_PADDING_SIZE;
445
        bytestream_put_buffer(&p, old.data, old.size);
446
        for (i=old.side_data_elems-1; i>=0; i--) {
447
            bytestream_put_buffer(&p, old.side_data[i].data, old.side_data[i].size);
448
            bytestream_put_be32(&p, old.side_data[i].size);
449
            *p++ = old.side_data[i].type | ((i==old.side_data_elems-1)*128);
450
        }
451
        bytestream_put_be64(&p, FF_MERGE_MARKER);
452
        av_assert0(p-pkt->data == pkt->size);
453
        memset(p, 0, AV_INPUT_BUFFER_PADDING_SIZE);
454
        av_packet_unref(&old);
455
        pkt->side_data_elems = 0;
456
        pkt->side_data = NULL;
457
        return 1;
458
    }
459
    return 0;
460
}
461
462
int av_packet_split_side_data(AVPacket *pkt){
463
    if (!pkt->side_data_elems && pkt->size >12 && AV_RB64(pkt->data + pkt->size - 8) == FF_MERGE_MARKER){
464
        int i;
465
        unsigned int size;
466
        uint8_t *p;
467
468
        p = pkt->data + pkt->size - 8 - 5;
469
        for (i=1; ; i++){
470
            size = AV_RB32(p);
471
            if (size>INT_MAX - 5 || p - pkt->data < size)
472
                return 0;
473
            if (p[4]&128)
474
                break;
475
            if (p - pkt->data < size + 5)
476
                return 0;
477
            p-= size+5;
478
        }
479
480
        if (i > AV_PKT_DATA_NB)
481
            return AVERROR(ERANGE);
482
483
        pkt->side_data = av_malloc_array(i, sizeof(*pkt->side_data));
484
        if (!pkt->side_data)
485
            return AVERROR(ENOMEM);
486
487
        p= pkt->data + pkt->size - 8 - 5;
488
        for (i=0; ; i++){
489
            size= AV_RB32(p);
490
            av_assert0(size<=INT_MAX - 5 && p - pkt->data >= size);
491
            pkt->side_data[i].data = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
492
            pkt->side_data[i].size = size;
493
            pkt->side_data[i].type = p[4]&127;
494
            if (!pkt->side_data[i].data)
495
                return AVERROR(ENOMEM);
496
            memcpy(pkt->side_data[i].data, p-size, size);
497
            pkt->size -= size + 5;
498
            if(p[4]&128)
499
                break;
500
            p-= size+5;
501
        }
502
        pkt->size -= 8;
503
        pkt->side_data_elems = i+1;
504
        return 1;
505
    }
506
    return 0;
507
}
508
#endif
509
510
#if FF_API_BUFFER_SIZE_T
511
577
uint8_t *av_packet_pack_dictionary(AVDictionary *dict, int *size)
512
#else
513
uint8_t *av_packet_pack_dictionary(AVDictionary *dict, size_t *size)
514
#endif
515
{
516
577
    uint8_t *data = NULL;
517
577
    *size = 0;
518
519
577
    if (!dict)
520
        return NULL;
521
522
1154
    for (int pass = 0; pass < 2; pass++) {
523
1154
        const AVDictionaryEntry *t = NULL;
524
1154
        size_t total_length = 0;
525
526
5772
        while ((t = av_dict_get(dict, "", t, AV_DICT_IGNORE_SUFFIX))) {
527
13854
            for (int i = 0; i < 2; i++) {
528
9236
                const char  *str = i ? t->value : t->key;
529
9236
                const size_t len = strlen(str) + 1;
530
531
9236
                if (pass)
532
4618
                    memcpy(data + total_length, str, len);
533
#if FF_API_BUFFER_SIZE_T
534
4618
                else if (len > INT_MAX - total_length)
535
#else
536
                else if (len > SIZE_MAX - total_length)
537
#endif
538
                    return NULL;
539
9236
                total_length += len;
540
            }
541
        }
542
1154
        if (pass)
543
577
            break;
544
577
        data = av_malloc(total_length);
545
577
        if (!data)
546
            return NULL;
547
577
        *size = total_length;
548
    }
549
550
577
    return data;
551
}
552
553
#if FF_API_BUFFER_SIZE_T
554
406358
int av_packet_unpack_dictionary(const uint8_t *data, int size, AVDictionary **dict)
555
#else
556
int av_packet_unpack_dictionary(const uint8_t *data, size_t size,
557
                                AVDictionary **dict)
558
#endif
559
{
560
    const uint8_t *end;
561
    int ret;
562
563

406358
    if (!dict || !data || !size)
564
405822
        return 0;
565
536
    end = data + size;
566

536
    if (size && end[-1])
567
        return AVERROR_INVALIDDATA;
568
2769
    while (data < end) {
569
2233
        const uint8_t *key = data;
570
2233
        const uint8_t *val = data + strlen(key) + 1;
571
572

2233
        if (val >= end || !*key)
573
            return AVERROR_INVALIDDATA;
574
575
2233
        ret = av_dict_set(dict, key, val, 0);
576
2233
        if (ret < 0)
577
            return ret;
578
2233
        data = val + strlen(val) + 1;
579
    }
580
581
536
    return 0;
582
}
583
584
int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
585
                               buffer_size_t size)
586
{
587
    int i;
588
589
    for (i = 0; i < pkt->side_data_elems; i++) {
590
        if (pkt->side_data[i].type == type) {
591
            if (size > pkt->side_data[i].size)
592
                return AVERROR(ENOMEM);
593
            pkt->side_data[i].size = size;
594
            return 0;
595
        }
596
    }
597
    return AVERROR(ENOENT);
598
}
599
600
1197040
int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
601
{
602
    int i;
603
604
1197040
    dst->pts                  = src->pts;
605
1197040
    dst->dts                  = src->dts;
606
1197040
    dst->pos                  = src->pos;
607
1197040
    dst->duration             = src->duration;
608
#if FF_API_CONVERGENCE_DURATION
609
FF_DISABLE_DEPRECATION_WARNINGS
610
1197040
    dst->convergence_duration = src->convergence_duration;
611
FF_ENABLE_DEPRECATION_WARNINGS
612
#endif
613
1197040
    dst->flags                = src->flags;
614
1197040
    dst->stream_index         = src->stream_index;
615
616
1197040
    dst->side_data            = NULL;
617
1197040
    dst->side_data_elems      = 0;
618
1216300
    for (i = 0; i < src->side_data_elems; i++) {
619
19260
        enum AVPacketSideDataType type = src->side_data[i].type;
620
19260
        buffer_size_t size = src->side_data[i].size;
621
19260
        uint8_t *src_data = src->side_data[i].data;
622
19260
        uint8_t *dst_data = av_packet_new_side_data(dst, type, size);
623
624
19260
        if (!dst_data) {
625
            av_packet_free_side_data(dst);
626
            return AVERROR(ENOMEM);
627
        }
628
19260
        memcpy(dst_data, src_data, size);
629
    }
630
631
1197040
    return 0;
632
}
633
634
6889920
void av_packet_unref(AVPacket *pkt)
635
{
636
6889920
    av_packet_free_side_data(pkt);
637
6889920
    av_buffer_unref(&pkt->buf);
638
6889920
    get_packet_defaults(pkt);
639
6889920
}
640
641
808814
int av_packet_ref(AVPacket *dst, const AVPacket *src)
642
{
643
    int ret;
644
645
808814
    dst->buf = NULL;
646
647
808814
    ret = av_packet_copy_props(dst, src);
648
808814
    if (ret < 0)
649
        goto fail;
650
651
808814
    if (!src->buf) {
652
1042
        ret = packet_alloc(&dst->buf, src->size);
653
1042
        if (ret < 0)
654
            goto fail;
655
        av_assert1(!src->size || src->data);
656
1042
        if (src->size)
657
1030
            memcpy(dst->buf->data, src->data, src->size);
658
659
1042
        dst->data = dst->buf->data;
660
    } else {
661
807772
        dst->buf = av_buffer_ref(src->buf);
662
807772
        if (!dst->buf) {
663
            ret = AVERROR(ENOMEM);
664
            goto fail;
665
        }
666
807772
        dst->data = src->data;
667
    }
668
669
808814
    dst->size = src->size;
670
671
808814
    return 0;
672
fail:
673
    av_packet_unref(dst);
674
    return ret;
675
}
676
677
1
AVPacket *av_packet_clone(const AVPacket *src)
678
{
679
1
    AVPacket *ret = av_packet_alloc();
680
681
1
    if (!ret)
682
        return ret;
683
684
1
    if (av_packet_ref(ret, src))
685
        av_packet_free(&ret);
686
687
1
    return ret;
688
}
689
690
1957538
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
691
{
692
1957538
    *dst = *src;
693
1957538
    get_packet_defaults(src);
694
1957538
}
695
696
2606465
int av_packet_make_refcounted(AVPacket *pkt)
697
{
698
    int ret;
699
700
2606465
    if (pkt->buf)
701
2439291
        return 0;
702
703
167174
    ret = packet_alloc(&pkt->buf, pkt->size);
704
167174
    if (ret < 0)
705
        return ret;
706
    av_assert1(!pkt->size || pkt->data);
707
167174
    if (pkt->size)
708
165686
        memcpy(pkt->buf->data, pkt->data, pkt->size);
709
710
167174
    pkt->data = pkt->buf->data;
711
712
167174
    return 0;
713
}
714
715
140
int av_packet_make_writable(AVPacket *pkt)
716
{
717
140
    AVBufferRef *buf = NULL;
718
    int ret;
719
720

140
    if (pkt->buf && av_buffer_is_writable(pkt->buf))
721
4
        return 0;
722
723
136
    ret = packet_alloc(&buf, pkt->size);
724
136
    if (ret < 0)
725
        return ret;
726
    av_assert1(!pkt->size || pkt->data);
727
136
    if (pkt->size)
728
136
        memcpy(buf->data, pkt->data, pkt->size);
729
730
136
    av_buffer_unref(&pkt->buf);
731
136
    pkt->buf  = buf;
732
136
    pkt->data = buf->data;
733
734
136
    return 0;
735
}
736
737
833608
void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
738
{
739
833608
    if (pkt->pts != AV_NOPTS_VALUE)
740
828139
        pkt->pts = av_rescale_q(pkt->pts, src_tb, dst_tb);
741
833608
    if (pkt->dts != AV_NOPTS_VALUE)
742
833604
        pkt->dts = av_rescale_q(pkt->dts, src_tb, dst_tb);
743
833608
    if (pkt->duration > 0)
744
643201
        pkt->duration = av_rescale_q(pkt->duration, src_tb, dst_tb);
745
#if FF_API_CONVERGENCE_DURATION
746
FF_DISABLE_DEPRECATION_WARNINGS
747
833608
    if (pkt->convergence_duration > 0)
748
4
        pkt->convergence_duration = av_rescale_q(pkt->convergence_duration, src_tb, dst_tb);
749
FF_ENABLE_DEPRECATION_WARNINGS
750
#endif
751
833608
}
752
753
345870
int avpriv_packet_list_put(PacketList **packet_buffer,
754
                           PacketList **plast_pktl,
755
                           AVPacket      *pkt,
756
                           int (*copy)(AVPacket *dst, const AVPacket *src),
757
                           int flags)
758
{
759
345870
    PacketList *pktl = av_mallocz(sizeof(PacketList));
760
    int ret;
761
762
345870
    if (!pktl)
763
        return AVERROR(ENOMEM);
764
765
345870
    if (copy) {
766
50
        ret = copy(&pktl->pkt, pkt);
767
50
        if (ret < 0) {
768
            av_free(pktl);
769
            return ret;
770
        }
771
    } else {
772
345820
        ret = av_packet_make_refcounted(pkt);
773
345820
        if (ret < 0) {
774
            av_free(pktl);
775
            return ret;
776
        }
777
345820
        av_packet_move_ref(&pktl->pkt, pkt);
778
    }
779
780
345870
    if (*packet_buffer)
781
204489
        (*plast_pktl)->next = pktl;
782
    else
783
141381
        *packet_buffer = pktl;
784
785
    /* Add the packet in the buffered packet list. */
786
345870
    *plast_pktl = pktl;
787
345870
    return 0;
788
}
789
790
253144
int avpriv_packet_list_get(PacketList **pkt_buffer,
791
                           PacketList **pkt_buffer_end,
792
                           AVPacket      *pkt)
793
{
794
    PacketList *pktl;
795
253144
    if (!*pkt_buffer)
796
        return AVERROR(EAGAIN);
797
253144
    pktl        = *pkt_buffer;
798
253144
    *pkt        = pktl->pkt;
799
253144
    *pkt_buffer = pktl->next;
800
253144
    if (!pktl->next)
801
137553
        *pkt_buffer_end = NULL;
802
253144
    av_freep(&pktl);
803
253144
    return 0;
804
}
805
806
72012
void avpriv_packet_list_free(PacketList **pkt_buf, PacketList **pkt_buf_end)
807
{
808
72012
    PacketList *tmp = *pkt_buf;
809
810
164738
    while (tmp) {
811
92726
        PacketList *pktl = tmp;
812
92726
        tmp = pktl->next;
813
92726
        av_packet_unref(&pktl->pkt);
814
92726
        av_freep(&pktl);
815
    }
816
72012
    *pkt_buf     = NULL;
817
72012
    *pkt_buf_end = NULL;
818
72012
}
819
820
9393
int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
821
{
822
    uint8_t *side_data;
823
    buffer_size_t side_data_size;
824
    int i;
825
826
9393
    side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, &side_data_size);
827
9393
    if (!side_data) {
828
9393
        side_data_size = 4+4+8*error_count;
829
9393
        side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
830
                                            side_data_size);
831
    }
832
833

9393
    if (!side_data || side_data_size < 4+4+8*error_count)
834
        return AVERROR(ENOMEM);
835
836
9393
    AV_WL32(side_data   , quality  );
837
9393
    side_data[4] = pict_type;
838
9393
    side_data[5] = error_count;
839
9393
    for (i = 0; i<error_count; i++)
840
        AV_WL64(side_data+8 + 8*i , error[i]);
841
842
9393
    return 0;
843
}
844
845
int ff_side_data_set_prft(AVPacket *pkt, int64_t timestamp)
846
{
847
    AVProducerReferenceTime *prft;
848
    uint8_t *side_data;
849
    buffer_size_t side_data_size;
850
851
    side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_PRFT, &side_data_size);
852
    if (!side_data) {
853
        side_data_size = sizeof(AVProducerReferenceTime);
854
        side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_PRFT, side_data_size);
855
    }
856
857
    if (!side_data || side_data_size < sizeof(AVProducerReferenceTime))
858
        return AVERROR(ENOMEM);
859
860
    prft = (AVProducerReferenceTime *)side_data;
861
    prft->wallclock = timestamp;
862
    prft->flags = 0;
863
864
    return 0;
865
}