GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/avpacket.c Lines: 243 443 54.9 %
Date: 2020-07-13 04:22:34 Branches: 103 259 39.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
10675599
void av_init_packet(AVPacket *pkt)
36
{
37
10675599
    pkt->pts                  = AV_NOPTS_VALUE;
38
10675599
    pkt->dts                  = AV_NOPTS_VALUE;
39
10675599
    pkt->pos                  = -1;
40
10675599
    pkt->duration             = 0;
41
#if FF_API_CONVERGENCE_DURATION
42
FF_DISABLE_DEPRECATION_WARNINGS
43
10675599
    pkt->convergence_duration = 0;
44
FF_ENABLE_DEPRECATION_WARNINGS
45
#endif
46
10675599
    pkt->flags                = 0;
47
10675599
    pkt->stream_index         = 0;
48
10675599
    pkt->buf                  = NULL;
49
10675599
    pkt->side_data            = NULL;
50
10675599
    pkt->side_data_elems      = 0;
51
10675599
}
52
53
90400
AVPacket *av_packet_alloc(void)
54
{
55
90400
    AVPacket *pkt = av_mallocz(sizeof(AVPacket));
56
90400
    if (!pkt)
57
        return pkt;
58
59
90400
    av_init_packet(pkt);
60
61
90400
    return pkt;
62
}
63
64
108488
void av_packet_free(AVPacket **pkt)
65
{
66

108488
    if (!pkt || !*pkt)
67
18984
        return;
68
69
89504
    av_packet_unref(*pkt);
70
89504
    av_freep(pkt);
71
}
72
73
1155462
static int packet_alloc(AVBufferRef **buf, int size)
74
{
75
    int ret;
76

1155462
    if (size < 0 || size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
77
1
        return AVERROR(EINVAL);
78
79
1155461
    ret = av_buffer_realloc(buf, size + AV_INPUT_BUFFER_PADDING_SIZE);
80
1155461
    if (ret < 0)
81
        return ret;
82
83
1155461
    memset((*buf)->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
84
85
1155461
    return 0;
86
}
87
88
995646
int av_new_packet(AVPacket *pkt, int size)
89
{
90
995646
    AVBufferRef *buf = NULL;
91
995646
    int ret = packet_alloc(&buf, size);
92
995646
    if (ret < 0)
93
1
        return ret;
94
95
995645
    av_init_packet(pkt);
96
995645
    pkt->buf      = buf;
97
995645
    pkt->data     = buf->data;
98
995645
    pkt->size     = size;
99
100
995645
    return 0;
101
}
102
103
497908
void av_shrink_packet(AVPacket *pkt, int size)
104
{
105
497908
    if (pkt->size <= size)
106
495645
        return;
107
2263
    pkt->size = size;
108
2263
    memset(pkt->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
109
}
110
111
316632
int av_grow_packet(AVPacket *pkt, int grow_by)
112
{
113
    int new_size;
114
316632
    av_assert0((unsigned)pkt->size <= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
115
316632
    if ((unsigned)grow_by >
116
316632
        INT_MAX - (pkt->size + AV_INPUT_BUFFER_PADDING_SIZE))
117
1
        return AVERROR(ENOMEM);
118
119
316631
    new_size = pkt->size + grow_by + AV_INPUT_BUFFER_PADDING_SIZE;
120
316631
    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
303341
        pkt->buf = av_buffer_alloc(new_size);
143
303341
        if (!pkt->buf)
144
            return AVERROR(ENOMEM);
145
303341
        if (pkt->size > 0)
146
            memcpy(pkt->buf->data, pkt->data, pkt->size);
147
303341
        pkt->data = pkt->buf->data;
148
    }
149
316631
    pkt->size += grow_by;
150
316631
    memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
151
152
316631
    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
5118320
void av_packet_free_side_data(AVPacket *pkt)
274
{
275
    int i;
276
5164279
    for (i = 0; i < pkt->side_data_elems; i++)
277
45959
        av_freep(&pkt->side_data[i].data);
278
5118320
    av_freep(&pkt->side_data);
279
5118320
    pkt->side_data_elems = 0;
280
5118320
}
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
45959
int av_packet_add_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
299
                            uint8_t *data, size_t size)
300
{
301
    AVPacketSideData *tmp;
302
45959
    int i, elems = pkt->side_data_elems;
303
304
46058
    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
45959
    if ((unsigned)elems + 1 > AV_PKT_DATA_NB)
316
        return AVERROR(ERANGE);
317
318
45959
    tmp = av_realloc(pkt->side_data, (elems + 1) * sizeof(*tmp));
319
45959
    if (!tmp)
320
        return AVERROR(ENOMEM);
321
322
45959
    pkt->side_data = tmp;
323
45959
    pkt->side_data[elems].data = data;
324
45959
    pkt->side_data[elems].size = size;
325
45959
    pkt->side_data[elems].type = type;
326
45959
    pkt->side_data_elems++;
327
328
45959
    return 0;
329
}
330
331
332
45105
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
45105
    if ((unsigned)size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
339
        return NULL;
340
45105
    data = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
341
45105
    if (!data)
342
        return NULL;
343
344
45105
    ret = av_packet_add_side_data(pkt, type, data, size);
345
45105
    if (ret < 0) {
346
        av_freep(&data);
347
        return NULL;
348
    }
349
350
45105
    return data;
351
}
352
353
5446237
uint8_t *av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type,
354
                                 int *size)
355
{
356
    int i;
357
358
5510769
    for (i = 0; i < pkt->side_data_elems; i++) {
359
75410
        if (pkt->side_data[i].type == type) {
360
10878
            if (size)
361
1564
                *size = pkt->side_data[i].size;
362
10878
            return pkt->side_data[i].data;
363
        }
364
    }
365
5435359
    if (size)
366
5308044
        *size = 0;
367
5435359
    return NULL;
368
}
369
370
209
const char *av_packet_side_data_name(enum AVPacketSideDataType type)
371
{
372







209
    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
    case AV_PKT_DATA_MASTERING_DISPLAY_METADATA: return "Mastering display metadata";
394
    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
    }
404
    return NULL;
405
}
406
407
#if FF_API_MERGE_SD_API
408
409
#define FF_MERGE_MARKER 0x8c4d9d108e25e9feULL
410
411
int av_packet_merge_side_data(AVPacket *pkt){
412
    if(pkt->side_data_elems){
413
        AVBufferRef *buf;
414
        int i;
415
        uint8_t *p;
416
        uint64_t size= pkt->size + 8LL + AV_INPUT_BUFFER_PADDING_SIZE;
417
        AVPacket old= *pkt;
418
        for (i=0; i<old.side_data_elems; i++) {
419
            size += old.side_data[i].size + 5LL;
420
        }
421
        if (size > INT_MAX)
422
            return AVERROR(EINVAL);
423
        buf = av_buffer_alloc(size);
424
        if (!buf)
425
            return AVERROR(ENOMEM);
426
        pkt->buf = buf;
427
        pkt->data = p = buf->data;
428
        pkt->size = size - AV_INPUT_BUFFER_PADDING_SIZE;
429
        bytestream_put_buffer(&p, old.data, old.size);
430
        for (i=old.side_data_elems-1; i>=0; i--) {
431
            bytestream_put_buffer(&p, old.side_data[i].data, old.side_data[i].size);
432
            bytestream_put_be32(&p, old.side_data[i].size);
433
            *p++ = old.side_data[i].type | ((i==old.side_data_elems-1)*128);
434
        }
435
        bytestream_put_be64(&p, FF_MERGE_MARKER);
436
        av_assert0(p-pkt->data == pkt->size);
437
        memset(p, 0, AV_INPUT_BUFFER_PADDING_SIZE);
438
        av_packet_unref(&old);
439
        pkt->side_data_elems = 0;
440
        pkt->side_data = NULL;
441
        return 1;
442
    }
443
    return 0;
444
}
445
446
int av_packet_split_side_data(AVPacket *pkt){
447
    if (!pkt->side_data_elems && pkt->size >12 && AV_RB64(pkt->data + pkt->size - 8) == FF_MERGE_MARKER){
448
        int i;
449
        unsigned int size;
450
        uint8_t *p;
451
452
        p = pkt->data + pkt->size - 8 - 5;
453
        for (i=1; ; i++){
454
            size = AV_RB32(p);
455
            if (size>INT_MAX - 5 || p - pkt->data < size)
456
                return 0;
457
            if (p[4]&128)
458
                break;
459
            if (p - pkt->data < size + 5)
460
                return 0;
461
            p-= size+5;
462
        }
463
464
        if (i > AV_PKT_DATA_NB)
465
            return AVERROR(ERANGE);
466
467
        pkt->side_data = av_malloc_array(i, sizeof(*pkt->side_data));
468
        if (!pkt->side_data)
469
            return AVERROR(ENOMEM);
470
471
        p= pkt->data + pkt->size - 8 - 5;
472
        for (i=0; ; i++){
473
            size= AV_RB32(p);
474
            av_assert0(size<=INT_MAX - 5 && p - pkt->data >= size);
475
            pkt->side_data[i].data = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
476
            pkt->side_data[i].size = size;
477
            pkt->side_data[i].type = p[4]&127;
478
            if (!pkt->side_data[i].data)
479
                return AVERROR(ENOMEM);
480
            memcpy(pkt->side_data[i].data, p-size, size);
481
            pkt->size -= size + 5;
482
            if(p[4]&128)
483
                break;
484
            p-= size+5;
485
        }
486
        pkt->size -= 8;
487
        pkt->side_data_elems = i+1;
488
        return 1;
489
    }
490
    return 0;
491
}
492
#endif
493
494
147
uint8_t *av_packet_pack_dictionary(AVDictionary *dict, int *size)
495
{
496
147
    AVDictionaryEntry *t = NULL;
497
147
    uint8_t *data = NULL;
498
147
    *size = 0;
499
500
147
    if (!dict)
501
        return NULL;
502
503
329
    while ((t = av_dict_get(dict, "", t, AV_DICT_IGNORE_SUFFIX))) {
504
182
        const size_t keylen   = strlen(t->key);
505
182
        const size_t valuelen = strlen(t->value);
506
182
        const size_t new_size = *size + keylen + 1 + valuelen + 1;
507
182
        uint8_t *const new_data = av_realloc(data, new_size);
508
509
182
        if (!new_data)
510
            goto fail;
511
182
        data = new_data;
512
182
        if (new_size > INT_MAX)
513
            goto fail;
514
515
182
        memcpy(data + *size, t->key, keylen + 1);
516
182
        memcpy(data + *size + keylen + 1, t->value, valuelen + 1);
517
518
182
        *size = new_size;
519
    }
520
521
147
    return data;
522
523
fail:
524
    av_freep(&data);
525
    *size = 0;
526
    return NULL;
527
}
528
529
441868
int av_packet_unpack_dictionary(const uint8_t *data, int size, AVDictionary **dict)
530
{
531
    const uint8_t *end;
532
    int ret;
533
534

441868
    if (!dict || !data || !size)
535
441332
        return 0;
536
536
    end = data + size;
537

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

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

131
    if (pkt->buf && av_buffer_is_writable(pkt->buf))
696
        return 0;
697
698
131
    ret = packet_alloc(&buf, pkt->size);
699
131
    if (ret < 0)
700
        return ret;
701
    av_assert1(!pkt->size || pkt->data);
702
131
    if (pkt->size)
703
131
        memcpy(buf->data, pkt->data, pkt->size);
704
705
131
    av_buffer_unref(&pkt->buf);
706
131
    pkt->buf  = buf;
707
131
    pkt->data = buf->data;
708
709
131
    return 0;
710
}
711
712
887292
void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
713
{
714
887292
    if (pkt->pts != AV_NOPTS_VALUE)
715
858508
        pkt->pts = av_rescale_q(pkt->pts, src_tb, dst_tb);
716
887292
    if (pkt->dts != AV_NOPTS_VALUE)
717
863960
        pkt->dts = av_rescale_q(pkt->dts, src_tb, dst_tb);
718
887292
    if (pkt->duration > 0)
719
697386
        pkt->duration = av_rescale_q(pkt->duration, src_tb, dst_tb);
720
#if FF_API_CONVERGENCE_DURATION
721
FF_DISABLE_DEPRECATION_WARNINGS
722
887292
    if (pkt->convergence_duration > 0)
723
        pkt->convergence_duration = av_rescale_q(pkt->convergence_duration, src_tb, dst_tb);
724
FF_ENABLE_DEPRECATION_WARNINGS
725
#endif
726
887292
}
727
728
9368
int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
729
{
730
    uint8_t *side_data;
731
    int side_data_size;
732
    int i;
733
734
9368
    side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS, &side_data_size);
735
9368
    if (!side_data) {
736
9368
        side_data_size = 4+4+8*error_count;
737
9368
        side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
738
                                            side_data_size);
739
    }
740
741

9368
    if (!side_data || side_data_size < 4+4+8*error_count)
742
        return AVERROR(ENOMEM);
743
744
9368
    AV_WL32(side_data   , quality  );
745
9368
    side_data[4] = pict_type;
746
9368
    side_data[5] = error_count;
747
9368
    for (i = 0; i<error_count; i++)
748
        AV_WL64(side_data+8 + 8*i , error[i]);
749
750
9368
    return 0;
751
}
752
753
int ff_side_data_set_prft(AVPacket *pkt, int64_t timestamp)
754
{
755
    AVProducerReferenceTime *prft;
756
    uint8_t *side_data;
757
    int side_data_size;
758
759
    side_data = av_packet_get_side_data(pkt, AV_PKT_DATA_PRFT, &side_data_size);
760
    if (!side_data) {
761
        side_data_size = sizeof(AVProducerReferenceTime);
762
        side_data = av_packet_new_side_data(pkt, AV_PKT_DATA_PRFT, side_data_size);
763
    }
764
765
    if (!side_data || side_data_size < sizeof(AVProducerReferenceTime))
766
        return AVERROR(ENOMEM);
767
768
    prft = (AVProducerReferenceTime *)side_data;
769
    prft->wallclock = timestamp;
770
    prft->flags = 0;
771
772
    return 0;
773
}