GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/avpacket.c Lines: 279 483 57.8 %
Date: 2020-09-21 17:35:45 Branches: 118 276 42.8 %

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
10370038
void av_init_packet(AVPacket *pkt)
36
{
37
10370038
    pkt->pts                  = AV_NOPTS_VALUE;
38
10370038
    pkt->dts                  = AV_NOPTS_VALUE;
39
10370038
    pkt->pos                  = -1;
40
10370038
    pkt->duration             = 0;
41
#if FF_API_CONVERGENCE_DURATION
42
FF_DISABLE_DEPRECATION_WARNINGS
43
10370038
    pkt->convergence_duration = 0;
44
FF_ENABLE_DEPRECATION_WARNINGS
45
#endif
46
10370038
    pkt->flags                = 0;
47
10370038
    pkt->stream_index         = 0;
48
10370038
    pkt->buf                  = NULL;
49
10370038
    pkt->side_data            = NULL;
50
10370038
    pkt->side_data_elems      = 0;
51
10370038
}
52
53
90761
AVPacket *av_packet_alloc(void)
54
{
55
90761
    AVPacket *pkt = av_mallocz(sizeof(AVPacket));
56
90761
    if (!pkt)
57
        return pkt;
58
59
90761
    av_init_packet(pkt);
60
61
90761
    return pkt;
62
}
63
64
108907
void av_packet_free(AVPacket **pkt)
65
{
66

108907
    if (!pkt || !*pkt)
67
19041
        return;
68
69
89866
    av_packet_unref(*pkt);
70
89866
    av_freep(pkt);
71
}
72
73
1143129
static int packet_alloc(AVBufferRef **buf, int size)
74
{
75
    int ret;
76

1143129
    if (size < 0 || size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
77
1
        return AVERROR(EINVAL);
78
79
1143128
    ret = av_buffer_realloc(buf, size + AV_INPUT_BUFFER_PADDING_SIZE);
80
1143128
    if (ret < 0)
81
        return ret;
82
83
1143128
    memset((*buf)->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
84
85
1143128
    return 0;
86
}
87
88
974621
int av_new_packet(AVPacket *pkt, int size)
89
{
90
974621
    AVBufferRef *buf = NULL;
91
974621
    int ret = packet_alloc(&buf, size);
92
974621
    if (ret < 0)
93
1
        return ret;
94
95
974620
    av_init_packet(pkt);
96
974620
    pkt->buf      = buf;
97
974620
    pkt->data     = buf->data;
98
974620
    pkt->size     = size;
99
100
974620
    return 0;
101
}
102
103
497927
void av_shrink_packet(AVPacket *pkt, int size)
104
{
105
497927
    if (pkt->size <= size)
106
495662
        return;
107
2265
    pkt->size = size;
108
2265
    memset(pkt->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
109
}
110
111
296165
int av_grow_packet(AVPacket *pkt, int grow_by)
112
{
113
    int new_size;
114
296165
    av_assert0((unsigned)pkt->size <= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
115
296165
    if ((unsigned)grow_by >
116
296165
        INT_MAX - (pkt->size + AV_INPUT_BUFFER_PADDING_SIZE))
117
1
        return AVERROR(ENOMEM);
118
119
296164
    new_size = pkt->size + grow_by + AV_INPUT_BUFFER_PADDING_SIZE;
120
296164
    if (pkt->buf) {
121
        size_t data_offset;
122
13290
        uint8_t *old_data = pkt->data;
123
13290
        if (pkt->data == NULL) {
124
            data_offset = 0;
125
            pkt->data = pkt->buf->data;
126
        } else {
127
13290
            data_offset = pkt->data - pkt->buf->data;
128
13290
            if (data_offset > INT_MAX - new_size)
129
                return AVERROR(ENOMEM);
130
        }
131
132

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








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

421531
    if (!dict || !data || !size)
536
420995
        return 0;
537
536
    end = data + size;
538

536
    if (size && end[-1])
539
        return AVERROR_INVALIDDATA;
540
2769
    while (data < end) {
541
2233
        const uint8_t *key = data;
542
2233
        const uint8_t *val = data + strlen(key) + 1;
543
544

2233
        if (val >= end || !*key)
545
            return AVERROR_INVALIDDATA;
546
547
2233
        ret = av_dict_set(dict, key, val, 0);
548
2233
        if (ret < 0)
549
            return ret;
550
2233
        data = val + strlen(val) + 1;
551
    }
552
553
536
    return 0;
554
}
555
556
int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
557
                               int size)
558
{
559
    int i;
560
561
    for (i = 0; i < pkt->side_data_elems; i++) {
562
        if (pkt->side_data[i].type == type) {
563
            if (size > pkt->side_data[i].size)
564
                return AVERROR(ENOMEM);
565
            pkt->side_data[i].size = size;
566
            return 0;
567
        }
568
    }
569
    return AVERROR(ENOENT);
570
}
571
572
830923
int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
573
{
574
    int i;
575
576
830923
    dst->pts                  = src->pts;
577
830923
    dst->dts                  = src->dts;
578
830923
    dst->pos                  = src->pos;
579
830923
    dst->duration             = src->duration;
580
#if FF_API_CONVERGENCE_DURATION
581
FF_DISABLE_DEPRECATION_WARNINGS
582
830923
    dst->convergence_duration = src->convergence_duration;
583
FF_ENABLE_DEPRECATION_WARNINGS
584
#endif
585
830923
    dst->flags                = src->flags;
586
830923
    dst->stream_index         = src->stream_index;
587
588
830923
    dst->side_data            = NULL;
589
830923
    dst->side_data_elems      = 0;
590
843676
    for (i = 0; i < src->side_data_elems; i++) {
591
12753
        enum AVPacketSideDataType type = src->side_data[i].type;
592
12753
        int size          = src->side_data[i].size;
593
12753
        uint8_t *src_data = src->side_data[i].data;
594
12753
        uint8_t *dst_data = av_packet_new_side_data(dst, type, size);
595
596
12753
        if (!dst_data) {
597
            av_packet_free_side_data(dst);
598
            return AVERROR(ENOMEM);
599
        }
600
12753
        memcpy(dst_data, src_data, size);
601
    }
602
603
830923
    return 0;
604
}
605
606
4950273
void av_packet_unref(AVPacket *pkt)
607
{
608
4950273
    av_packet_free_side_data(pkt);
609
4950273
    av_buffer_unref(&pkt->buf);
610
4950273
    av_init_packet(pkt);
611
4950273
    pkt->data = NULL;
612
4950273
    pkt->size = 0;
613
4950273
}
614
615
426084
int av_packet_ref(AVPacket *dst, const AVPacket *src)
616
{
617
    int ret;
618
619
426084
    dst->buf = NULL;
620
621
426084
    ret = av_packet_copy_props(dst, src);
622
426084
    if (ret < 0)
623
        goto fail;
624
625
426084
    if (!src->buf) {
626
1042
        ret = packet_alloc(&dst->buf, src->size);
627
1042
        if (ret < 0)
628
            goto fail;
629
        av_assert1(!src->size || src->data);
630
1042
        if (src->size)
631
1030
            memcpy(dst->buf->data, src->data, src->size);
632
633
1042
        dst->data = dst->buf->data;
634
    } else {
635
425042
        dst->buf = av_buffer_ref(src->buf);
636
425042
        if (!dst->buf) {
637
            ret = AVERROR(ENOMEM);
638
            goto fail;
639
        }
640
425042
        dst->data = src->data;
641
    }
642
643
426084
    dst->size = src->size;
644
645
426084
    return 0;
646
fail:
647
    av_packet_unref(dst);
648
    return ret;
649
}
650
651
1
AVPacket *av_packet_clone(const AVPacket *src)
652
{
653
1
    AVPacket *ret = av_packet_alloc();
654
655
1
    if (!ret)
656
        return ret;
657
658
1
    if (av_packet_ref(ret, src))
659
        av_packet_free(&ret);
660
661
1
    return ret;
662
}
663
664
2009723
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
665
{
666
2009723
    *dst = *src;
667
2009723
    av_init_packet(src);
668
2009723
    src->data = NULL;
669
2009723
    src->size = 0;
670
2009723
}
671
672
2656826
int av_packet_make_refcounted(AVPacket *pkt)
673
{
674
    int ret;
675
676
2656826
    if (pkt->buf)
677
2489491
        return 0;
678
679
167335
    ret = packet_alloc(&pkt->buf, pkt->size);
680
167335
    if (ret < 0)
681
        return ret;
682
    av_assert1(!pkt->size || pkt->data);
683
167335
    if (pkt->size)
684
165775
        memcpy(pkt->buf->data, pkt->data, pkt->size);
685
686
167335
    pkt->data = pkt->buf->data;
687
688
167335
    return 0;
689
}
690
691
131
int av_packet_make_writable(AVPacket *pkt)
692
{
693
131
    AVBufferRef *buf = NULL;
694
    int ret;
695
696

131
    if (pkt->buf && av_buffer_is_writable(pkt->buf))
697
        return 0;
698
699
131
    ret = packet_alloc(&buf, pkt->size);
700
131
    if (ret < 0)
701
        return ret;
702
    av_assert1(!pkt->size || pkt->data);
703
131
    if (pkt->size)
704
131
        memcpy(buf->data, pkt->data, pkt->size);
705
706
131
    av_buffer_unref(&pkt->buf);
707
131
    pkt->buf  = buf;
708
131
    pkt->data = buf->data;
709
710
131
    return 0;
711
}
712
713
862508
void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
714
{
715
862508
    if (pkt->pts != AV_NOPTS_VALUE)
716
833724
        pkt->pts = av_rescale_q(pkt->pts, src_tb, dst_tb);
717
862508
    if (pkt->dts != AV_NOPTS_VALUE)
718
839176
        pkt->dts = av_rescale_q(pkt->dts, src_tb, dst_tb);
719
862508
    if (pkt->duration > 0)
720
672584
        pkt->duration = av_rescale_q(pkt->duration, src_tb, dst_tb);
721
#if FF_API_CONVERGENCE_DURATION
722
FF_DISABLE_DEPRECATION_WARNINGS
723
862508
    if (pkt->convergence_duration > 0)
724
        pkt->convergence_duration = av_rescale_q(pkt->convergence_duration, src_tb, dst_tb);
725
FF_ENABLE_DEPRECATION_WARNINGS
726
#endif
727
862508
}
728
729
733923
int avpriv_packet_list_put(AVPacketList **packet_buffer,
730
                           AVPacketList **plast_pktl,
731
                           AVPacket      *pkt,
732
                           int (*copy)(AVPacket *dst, const AVPacket *src),
733
                           int flags)
734
{
735
733923
    AVPacketList *pktl = av_mallocz(sizeof(AVPacketList));
736
    int ret;
737
738
733923
    if (!pktl)
739
        return AVERROR(ENOMEM);
740
741
733923
    if (copy) {
742
391386
        ret = copy(&pktl->pkt, pkt);
743
391386
        if (ret < 0) {
744
            av_free(pktl);
745
            return ret;
746
        }
747
    } else {
748
342537
        ret = av_packet_make_refcounted(pkt);
749
342537
        if (ret < 0) {
750
            av_free(pktl);
751
            return ret;
752
        }
753
342537
        av_packet_move_ref(&pktl->pkt, pkt);
754
    }
755
756
733923
    if (*packet_buffer)
757
204124
        (*plast_pktl)->next = pktl;
758
    else
759
529799
        *packet_buffer = pktl;
760
761
    /* Add the packet in the buffered packet list. */
762
733923
    *plast_pktl = pktl;
763
733923
    return 0;
764
}
765
766
649490
int avpriv_packet_list_get(AVPacketList **pkt_buffer,
767
                           AVPacketList **pkt_buffer_end,
768
                           AVPacket      *pkt)
769
{
770
    AVPacketList *pktl;
771
649490
    if (!*pkt_buffer)
772
9013
        return AVERROR(EAGAIN);
773
640477
    pktl        = *pkt_buffer;
774
640477
    *pkt        = pktl->pkt;
775
640477
    *pkt_buffer = pktl->next;
776
640477
    if (!pktl->next)
777
525956
        *pkt_buffer_end = NULL;
778
640477
    av_freep(&pktl);
779
640477
    return 0;
780
}
781
782
90139
void avpriv_packet_list_free(AVPacketList **pkt_buf, AVPacketList **pkt_buf_end)
783
{
784
90139
    AVPacketList *tmp = *pkt_buf;
785
786
183585
    while (tmp) {
787
93446
        AVPacketList *pktl = tmp;
788
93446
        tmp = pktl->next;
789
93446
        av_packet_unref(&pktl->pkt);
790
93446
        av_freep(&pktl);
791
    }
792
90139
    *pkt_buf     = NULL;
793
90139
    *pkt_buf_end = NULL;
794
90139
}
795
796
9368
int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
797
{
798
    uint8_t *side_data;
799
    int side_data_size;
800
    int i;
801
802
9368
    side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, &side_data_size);
803
9368
    if (!side_data) {
804
9368
        side_data_size = 4+4+8*error_count;
805
9368
        side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
806
                                            side_data_size);
807
    }
808
809

9368
    if (!side_data || side_data_size < 4+4+8*error_count)
810
        return AVERROR(ENOMEM);
811
812
9368
    AV_WL32(side_data   , quality  );
813
9368
    side_data[4] = pict_type;
814
9368
    side_data[5] = error_count;
815
9368
    for (i = 0; i<error_count; i++)
816
        AV_WL64(side_data+8 + 8*i , error[i]);
817
818
9368
    return 0;
819
}
820
821
int ff_side_data_set_prft(AVPacket *pkt, int64_t timestamp)
822
{
823
    AVProducerReferenceTime *prft;
824
    uint8_t *side_data;
825
    int side_data_size;
826
827
    side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_PRFT, &side_data_size);
828
    if (!side_data) {
829
        side_data_size = sizeof(AVProducerReferenceTime);
830
        side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_PRFT, side_data_size);
831
    }
832
833
    if (!side_data || side_data_size < sizeof(AVProducerReferenceTime))
834
        return AVERROR(ENOMEM);
835
836
    prft = (AVProducerReferenceTime *)side_data;
837
    prft->wallclock = timestamp;
838
    prft->flags = 0;
839
840
    return 0;
841
}