GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/avpacket.c Lines: 240 428 56.1 %
Date: 2019-11-18 18:00:01 Branches: 99 248 39.9 %

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

88276
    if (!pkt || !*pkt)
65
18591
        return;
66
67
69685
    av_packet_unref(*pkt);
68
69685
    av_freep(pkt);
69
}
70
71
1127448
static int packet_alloc(AVBufferRef **buf, int size)
72
{
73
    int ret;
74

1127448
    if (size < 0 || size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
75
1
        return AVERROR(EINVAL);
76
77
1127447
    ret = av_buffer_realloc(buf, size + AV_INPUT_BUFFER_PADDING_SIZE);
78
1127447
    if (ret < 0)
79
        return ret;
80
81
1127447
    memset((*buf)->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
82
83
1127447
    return 0;
84
}
85
86
968919
int av_new_packet(AVPacket *pkt, int size)
87
{
88
968919
    AVBufferRef *buf = NULL;
89
968919
    int ret = packet_alloc(&buf, size);
90
968919
    if (ret < 0)
91
1
        return ret;
92
93
968918
    av_init_packet(pkt);
94
968918
    pkt->buf      = buf;
95
968918
    pkt->data     = buf->data;
96
968918
    pkt->size     = size;
97
98
968918
    return 0;
99
}
100
101
496761
void av_shrink_packet(AVPacket *pkt, int size)
102
{
103
496761
    if (pkt->size <= size)
104
494532
        return;
105
2229
    pkt->size = size;
106
2229
    memset(pkt->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
107
}
108
109
293366
int av_grow_packet(AVPacket *pkt, int grow_by)
110
{
111
    int new_size;
112
293366
    av_assert0((unsigned)pkt->size <= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
113
293366
    if ((unsigned)grow_by >
114
293366
        INT_MAX - (pkt->size + AV_INPUT_BUFFER_PADDING_SIZE))
115
1
        return AVERROR(ENOMEM);
116
117
293365
    new_size = pkt->size + grow_by + AV_INPUT_BUFFER_PADDING_SIZE;
118
293365
    if (pkt->buf) {
119
        size_t data_offset;
120
14489
        uint8_t *old_data = pkt->data;
121
14489
        if (pkt->data == NULL) {
122
            data_offset = 0;
123
            pkt->data = pkt->buf->data;
124
        } else {
125
14489
            data_offset = pkt->data - pkt->buf->data;
126
14489
            if (data_offset > INT_MAX - new_size)
127
                return AVERROR(ENOMEM);
128
        }
129
130
14489
        if (new_size + data_offset > pkt->buf->size) {
131
14489
            int ret = av_buffer_realloc(&pkt->buf, new_size + data_offset);
132
14489
            if (ret < 0) {
133
                pkt->data = old_data;
134
                return ret;
135
            }
136
14489
            pkt->data = pkt->buf->data + data_offset;
137
        }
138
    } else {
139
278876
        pkt->buf = av_buffer_alloc(new_size);
140
278876
        if (!pkt->buf)
141
            return AVERROR(ENOMEM);
142
278876
        if (pkt->size > 0)
143
            memcpy(pkt->buf->data, pkt->data, pkt->size);
144
278876
        pkt->data = pkt->buf->data;
145
    }
146
293365
    pkt->size += grow_by;
147
293365
    memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
148
149
293365
    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
5685051
void av_packet_free_side_data(AVPacket *pkt)
271
{
272
    int i;
273
5740980
    for (i = 0; i < pkt->side_data_elems; i++)
274
55929
        av_freep(&pkt->side_data[i].data);
275
5685051
    av_freep(&pkt->side_data);
276
5685051
    pkt->side_data_elems = 0;
277
5685051
}
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
55929
int av_packet_add_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
296
                            uint8_t *data, size_t size)
297
{
298
    AVPacketSideData *tmp;
299
55929
    int i, elems = pkt->side_data_elems;
300
301
56015
    for (i = 0; i < elems; i++) {
302
86
        AVPacketSideData *sd = &pkt->side_data[i];
303
304
86
        if (sd->type == type) {
305
            av_free(sd->data);
306
            sd->data = data;
307
            sd->size = size;
308
            return 0;
309
        }
310
    }
311
312
55929
    if ((unsigned)elems + 1 > AV_PKT_DATA_NB)
313
        return AVERROR(ERANGE);
314
315
55929
    tmp = av_realloc(pkt->side_data, (elems + 1) * sizeof(*tmp));
316
55929
    if (!tmp)
317
        return AVERROR(ENOMEM);
318
319
55929
    pkt->side_data = tmp;
320
55929
    pkt->side_data[elems].data = data;
321
55929
    pkt->side_data[elems].size = size;
322
55929
    pkt->side_data[elems].type = type;
323
55929
    pkt->side_data_elems++;
324
325
55929
    return 0;
326
}
327
328
329
55258
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
55258
    if ((unsigned)size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
336
        return NULL;
337
55258
    data = av_mallocz(size + AV_INPUT_BUFFER_PADDING_SIZE);
338
55258
    if (!data)
339
        return NULL;
340
341
55258
    ret = av_packet_add_side_data(pkt, type, data, size);
342
55258
    if (ret < 0) {
343
        av_freep(&data);
344
        return NULL;
345
    }
346
347
55258
    return data;
348
}
349
350
4696272
uint8_t *av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type,
351
                                 int *size)
352
{
353
    int i;
354
355
4753879
    for (i = 0; i < pkt->side_data_elems; i++) {
356
68446
        if (pkt->side_data[i].type == type) {
357
10839
            if (size)
358
1525
                *size = pkt->side_data[i].size;
359
10839
            return pkt->side_data[i].data;
360
        }
361
    }
362
4685433
    if (size)
363
4582234
        *size = 0;
364
4685433
    return NULL;
365
}
366
367
207
const char *av_packet_side_data_name(enum AVPacketSideDataType type)
368
{
369







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

417853
    if (!dict || !data || !size)
529
417330
        return ret;
530
523
    end = data + size;
531

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

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

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

9368
    if (!side_data || side_data_size < 4+4+8*error_count)
734
        return AVERROR(ENOMEM);
735
736
9368
    AV_WL32(side_data   , quality  );
737
9368
    side_data[4] = pict_type;
738
9368
    side_data[5] = error_count;
739
9368
    for (i = 0; i<error_count; i++)
740
        AV_WL64(side_data+8 + 8*i , error[i]);
741
742
9368
    return 0;
743
}