GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/avpacket.c Lines: 242 441 54.9 %
Date: 2020-04-04 00:26:16 Branches: 101 255 39.6 %

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
#include "avcodec.h"
30
#include "bytestream.h"
31
#include "internal.h"
32
33
11989790
void av_init_packet(AVPacket *pkt)
34
{
35
11989790
    pkt->pts                  = AV_NOPTS_VALUE;
36
11989790
    pkt->dts                  = AV_NOPTS_VALUE;
37
11989790
    pkt->pos                  = -1;
38
11989790
    pkt->duration             = 0;
39
#if FF_API_CONVERGENCE_DURATION
40
FF_DISABLE_DEPRECATION_WARNINGS
41
11989790
    pkt->convergence_duration = 0;
42
FF_ENABLE_DEPRECATION_WARNINGS
43
#endif
44
11989790
    pkt->flags                = 0;
45
11989790
    pkt->stream_index         = 0;
46
11989790
    pkt->buf                  = NULL;
47
11989790
    pkt->side_data            = NULL;
48
11989790
    pkt->side_data_elems      = 0;
49
11989790
}
50
51
70845
AVPacket *av_packet_alloc(void)
52
{
53
70845
    AVPacket *pkt = av_mallocz(sizeof(AVPacket));
54
70845
    if (!pkt)
55
        return pkt;
56
57
70845
    av_init_packet(pkt);
58
59
70845
    return pkt;
60
}
61
62
88663
void av_packet_free(AVPacket **pkt)
63
{
64

88663
    if (!pkt || !*pkt)
65
18715
        return;
66
67
69948
    av_packet_unref(*pkt);
68
69948
    av_freep(pkt);
69
}
70
71
1151089
static int packet_alloc(AVBufferRef **buf, int size)
72
{
73
    int ret;
74

1151089
    if (size < 0 || size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
75
1
        return AVERROR(EINVAL);
76
77
1151088
    ret = av_buffer_realloc(buf, size + AV_INPUT_BUFFER_PADDING_SIZE);
78
1151088
    if (ret < 0)
79
        return ret;
80
81
1151088
    memset((*buf)->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
82
83
1151088
    return 0;
84
}
85
86
992076
int av_new_packet(AVPacket *pkt, int size)
87
{
88
992076
    AVBufferRef *buf = NULL;
89
992076
    int ret = packet_alloc(&buf, size);
90
992076
    if (ret < 0)
91
1
        return ret;
92
93
992075
    av_init_packet(pkt);
94
992075
    pkt->buf      = buf;
95
992075
    pkt->data     = buf->data;
96
992075
    pkt->size     = size;
97
98
992075
    return 0;
99
}
100
101
497329
void av_shrink_packet(AVPacket *pkt, int size)
102
{
103
497329
    if (pkt->size <= size)
104
495082
        return;
105
2247
    pkt->size = size;
106
2247
    memset(pkt->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
107
}
108
109
314650
int av_grow_packet(AVPacket *pkt, int grow_by)
110
{
111
    int new_size;
112
314650
    av_assert0((unsigned)pkt->size <= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
113
314650
    if ((unsigned)grow_by >
114
314650
        INT_MAX - (pkt->size + AV_INPUT_BUFFER_PADDING_SIZE))
115
1
        return AVERROR(ENOMEM);
116
117
314649
    new_size = pkt->size + grow_by + AV_INPUT_BUFFER_PADDING_SIZE;
118
314649
    if (pkt->buf) {
119
        size_t data_offset;
120
13290
        uint8_t *old_data = pkt->data;
121
13290
        if (pkt->data == NULL) {
122
            data_offset = 0;
123
            pkt->data = pkt->buf->data;
124
        } else {
125
13290
            data_offset = pkt->data - pkt->buf->data;
126
13290
            if (data_offset > INT_MAX - new_size)
127
                return AVERROR(ENOMEM);
128
        }
129
130
13290
        if (new_size + data_offset > pkt->buf->size) {
131
13289
            int ret = av_buffer_realloc(&pkt->buf, new_size + data_offset);
132
13289
            if (ret < 0) {
133
                pkt->data = old_data;
134
                return ret;
135
            }
136
13289
            pkt->data = pkt->buf->data + data_offset;
137
        }
138
    } else {
139
301359
        pkt->buf = av_buffer_alloc(new_size);
140
301359
        if (!pkt->buf)
141
            return AVERROR(ENOMEM);
142
301359
        if (pkt->size > 0)
143
            memcpy(pkt->buf->data, pkt->data, pkt->size);
144
301359
        pkt->data = pkt->buf->data;
145
    }
146
314649
    pkt->size += grow_by;
147
314649
    memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
148
149
314649
    return 0;
150
}
151
152
52
int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
153
{
154
52
    if (size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
155
1
        return AVERROR(EINVAL);
156
157
51
    pkt->buf = av_buffer_create(data, size + AV_INPUT_BUFFER_PADDING_SIZE,
158
                                av_buffer_default_free, NULL, 0);
159
51
    if (!pkt->buf)
160
        return AVERROR(ENOMEM);
161
162
51
    pkt->data = data;
163
51
    pkt->size = size;
164
165
51
    return 0;
166
}
167
168
#if FF_API_AVPACKET_OLD_API
169
FF_DISABLE_DEPRECATION_WARNINGS
170
#define ALLOC_MALLOC(data, size) data = av_malloc(size)
171
#define ALLOC_BUF(data, size)                \
172
do {                                         \
173
    av_buffer_realloc(&pkt->buf, size);      \
174
    data = pkt->buf ? pkt->buf->data : NULL; \
175
} while (0)
176
177
#define DUP_DATA(dst, src, size, padding, ALLOC)                        \
178
    do {                                                                \
179
        void *data;                                                     \
180
        if (padding) {                                                  \
181
            if ((unsigned)(size) >                                      \
182
                (unsigned)(size) + AV_INPUT_BUFFER_PADDING_SIZE)        \
183
                goto failed_alloc;                                      \
184
            ALLOC(data, size + AV_INPUT_BUFFER_PADDING_SIZE);           \
185
        } else {                                                        \
186
            ALLOC(data, size);                                          \
187
        }                                                               \
188
        if (!data)                                                      \
189
            goto failed_alloc;                                          \
190
        memcpy(data, src, size);                                        \
191
        if (padding)                                                    \
192
            memset((uint8_t *)data + size, 0,                           \
193
                   AV_INPUT_BUFFER_PADDING_SIZE);                       \
194
        dst = data;                                                     \
195
    } while (0)
196
197
/* Makes duplicates of data, side_data, but does not copy any other fields */
198
static int copy_packet_data(AVPacket *pkt, const AVPacket *src, int dup)
199
{
200
    pkt->data      = NULL;
201
    pkt->side_data = NULL;
202
    pkt->side_data_elems = 0;
203
    if (pkt->buf) {
204
        AVBufferRef *ref = av_buffer_ref(src->buf);
205
        if (!ref)
206
            return AVERROR(ENOMEM);
207
        pkt->buf  = ref;
208
        pkt->data = ref->data;
209
    } else {
210
        DUP_DATA(pkt->data, src->data, pkt->size, 1, ALLOC_BUF);
211
    }
212
    if (src->side_data_elems && dup) {
213
        pkt->side_data = src->side_data;
214
        pkt->side_data_elems = src->side_data_elems;
215
    }
216
    if (src->side_data_elems && !dup) {
217
        return av_copy_packet_side_data(pkt, src);
218
    }
219
    return 0;
220
221
failed_alloc:
222
    av_packet_unref(pkt);
223
    return AVERROR(ENOMEM);
224
}
225
226
int av_copy_packet_side_data(AVPacket *pkt, const AVPacket *src)
227
{
228
    if (src->side_data_elems) {
229
        int i;
230
        DUP_DATA(pkt->side_data, src->side_data,
231
                src->side_data_elems * sizeof(*src->side_data), 0, ALLOC_MALLOC);
232
        if (src != pkt) {
233
            memset(pkt->side_data, 0,
234
                   src->side_data_elems * sizeof(*src->side_data));
235
        }
236
        for (i = 0; i < src->side_data_elems; i++) {
237
            DUP_DATA(pkt->side_data[i].data, src->side_data[i].data,
238
                    src->side_data[i].size, 1, ALLOC_MALLOC);
239
            pkt->side_data[i].size = src->side_data[i].size;
240
            pkt->side_data[i].type = src->side_data[i].type;
241
        }
242
    }
243
    pkt->side_data_elems = src->side_data_elems;
244
    return 0;
245
246
failed_alloc:
247
    av_packet_unref(pkt);
248
    return AVERROR(ENOMEM);
249
}
250
251
int av_dup_packet(AVPacket *pkt)
252
{
253
    AVPacket tmp_pkt;
254
255
    if (!pkt->buf && pkt->data) {
256
        tmp_pkt = *pkt;
257
        return copy_packet_data(pkt, &tmp_pkt, 1);
258
    }
259
    return 0;
260
}
261
262
int av_copy_packet(AVPacket *dst, const AVPacket *src)
263
{
264
    *dst = *src;
265
    return copy_packet_data(dst, src, 0);
266
}
267
FF_ENABLE_DEPRECATION_WARNINGS
268
#endif
269
270
5505452
void av_packet_free_side_data(AVPacket *pkt)
271
{
272
    int i;
273
5551311
    for (i = 0; i < pkt->side_data_elems; i++)
274
45859
        av_freep(&pkt->side_data[i].data);
275
5505452
    av_freep(&pkt->side_data);
276
5505452
    pkt->side_data_elems = 0;
277
5505452
}
278
279
#if FF_API_AVPACKET_OLD_API
280
FF_DISABLE_DEPRECATION_WARNINGS
281
void av_free_packet(AVPacket *pkt)
282
{
283
    if (pkt) {
284
        if (pkt->buf)
285
            av_buffer_unref(&pkt->buf);
286
        pkt->data            = NULL;
287
        pkt->size            = 0;
288
289
        av_packet_free_side_data(pkt);
290
    }
291
}
292
FF_ENABLE_DEPRECATION_WARNINGS
293
#endif
294
295
45859
int av_packet_add_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
296
                            uint8_t *data, size_t size)
297
{
298
    AVPacketSideData *tmp;
299
45859
    int i, elems = pkt->side_data_elems;
300
301
45958
    for (i = 0; i < elems; i++) {
302
99
        AVPacketSideData *sd = &pkt->side_data[i];
303
304
99
        if (sd->type == type) {
305
            av_free(sd->data);
306
            sd->data = data;
307
            sd->size = size;
308
            return 0;
309
        }
310
    }
311
312
45859
    if ((unsigned)elems + 1 > AV_PKT_DATA_NB)
313
        return AVERROR(ERANGE);
314
315
45859
    tmp = av_realloc(pkt->side_data, (elems + 1) * sizeof(*tmp));
316
45859
    if (!tmp)
317
        return AVERROR(ENOMEM);
318
319
45859
    pkt->side_data = tmp;
320
45859
    pkt->side_data[elems].data = data;
321
45859
    pkt->side_data[elems].size = size;
322
45859
    pkt->side_data[elems].type = type;
323
45859
    pkt->side_data_elems++;
324
325
45859
    return 0;
326
}
327
328
329
45167
uint8_t *av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
330
                                 int size)
331
{
332
    int ret;
333
    uint8_t *data;
334
335
45167
    if ((unsigned)size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
336
        return NULL;
337
45167
    data = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
338
45167
    if (!data)
339
        return NULL;
340
341
45167
    ret = av_packet_add_side_data(pkt, type, data, size);
342
45167
    if (ret < 0) {
343
        av_freep(&data);
344
        return NULL;
345
    }
346
347
45167
    return data;
348
}
349
350
5395531
uint8_t *av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type,
351
                                 int *size)
352
{
353
    int i;
354
355
5459718
    for (i = 0; i < pkt->side_data_elems; i++) {
356
75049
        if (pkt->side_data[i].type == type) {
357
10862
            if (size)
358
1548
                *size = pkt->side_data[i].size;
359
10862
            return pkt->side_data[i].data;
360
        }
361
    }
362
5384669
    if (size)
363
5280053
        *size = 0;
364
5384669
    return NULL;
365
}
366
367
208
const char *av_packet_side_data_name(enum AVPacketSideDataType type)
368
{
369







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

440015
    if (!dict || !data || !size)
530
439492
        return ret;
531
523
    end = data + size;
532

523
    if (size && end[-1])
533
        return AVERROR_INVALIDDATA;
534
2667
    while (data < end) {
535
2144
        const uint8_t *key = data;
536
2144
        const uint8_t *val = data + strlen(key) + 1;
537
538

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

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

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