GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/codec_par.c Lines: 128 133 96.2 %
Date: 2021-04-20 04:37:23 Branches: 19 24 79.2 %

Line Branch Exec Source
1
/*
2
 * AVCodecParameters functions for libavcodec
3
 *
4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with FFmpeg; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
/**
22
 * @file
23
 * AVCodecParameters functions for libavcodec.
24
 */
25
26
#include <string.h>
27
#include "libavutil/mem.h"
28
#include "avcodec.h"
29
#include "codec_par.h"
30
31
157206
static void codec_parameters_reset(AVCodecParameters *par)
32
{
33
157206
    av_freep(&par->extradata);
34
35
157206
    memset(par, 0, sizeof(*par));
36
37
157206
    par->codec_type          = AVMEDIA_TYPE_UNKNOWN;
38
157206
    par->codec_id            = AV_CODEC_ID_NONE;
39
157206
    par->format              = -1;
40
157206
    par->field_order         = AV_FIELD_UNKNOWN;
41
157206
    par->color_range         = AVCOL_RANGE_UNSPECIFIED;
42
157206
    par->color_primaries     = AVCOL_PRI_UNSPECIFIED;
43
157206
    par->color_trc           = AVCOL_TRC_UNSPECIFIED;
44
157206
    par->color_space         = AVCOL_SPC_UNSPECIFIED;
45
157206
    par->chroma_location     = AVCHROMA_LOC_UNSPECIFIED;
46
157206
    par->sample_aspect_ratio = (AVRational){ 0, 1 };
47
157206
    par->profile             = FF_PROFILE_UNKNOWN;
48
157206
    par->level               = FF_LEVEL_UNKNOWN;
49
157206
}
50
51
48838
AVCodecParameters *avcodec_parameters_alloc(void)
52
{
53
48838
    AVCodecParameters *par = av_mallocz(sizeof(*par));
54
55
48838
    if (!par)
56
        return NULL;
57
48838
    codec_parameters_reset(par);
58
48838
    return par;
59
}
60
61
48832
void avcodec_parameters_free(AVCodecParameters **ppar)
62
{
63
48832
    AVCodecParameters *par = *ppar;
64
65
48832
    if (!par)
66
        return;
67
48832
    codec_parameters_reset(par);
68
69
48832
    av_freep(ppar);
70
}
71
72
27274
int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
73
{
74
27274
    codec_parameters_reset(dst);
75
27274
    memcpy(dst, src, sizeof(*dst));
76
77
27274
    dst->extradata      = NULL;
78
27274
    dst->extradata_size = 0;
79
27274
    if (src->extradata) {
80
4224
        dst->extradata = av_mallocz(src->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
81
4224
        if (!dst->extradata)
82
            return AVERROR(ENOMEM);
83
4224
        memcpy(dst->extradata, src->extradata, src->extradata_size);
84
4224
        dst->extradata_size = src->extradata_size;
85
    }
86
87
27274
    return 0;
88
}
89
90
32262
int avcodec_parameters_from_context(AVCodecParameters *par,
91
                                    const AVCodecContext *codec)
92
{
93
32262
    codec_parameters_reset(par);
94
95
32262
    par->codec_type = codec->codec_type;
96
32262
    par->codec_id   = codec->codec_id;
97
32262
    par->codec_tag  = codec->codec_tag;
98
99
32262
    par->bit_rate              = codec->bit_rate;
100
32262
    par->bits_per_coded_sample = codec->bits_per_coded_sample;
101
32262
    par->bits_per_raw_sample   = codec->bits_per_raw_sample;
102
32262
    par->profile               = codec->profile;
103
32262
    par->level                 = codec->level;
104
105

32262
    switch (par->codec_type) {
106
24806
    case AVMEDIA_TYPE_VIDEO:
107
24806
        par->format              = codec->pix_fmt;
108
24806
        par->width               = codec->width;
109
24806
        par->height              = codec->height;
110
24806
        par->field_order         = codec->field_order;
111
24806
        par->color_range         = codec->color_range;
112
24806
        par->color_primaries     = codec->color_primaries;
113
24806
        par->color_trc           = codec->color_trc;
114
24806
        par->color_space         = codec->colorspace;
115
24806
        par->chroma_location     = codec->chroma_sample_location;
116
24806
        par->sample_aspect_ratio = codec->sample_aspect_ratio;
117
24806
        par->video_delay         = codec->has_b_frames;
118
24806
        break;
119
7079
    case AVMEDIA_TYPE_AUDIO:
120
7079
        par->format           = codec->sample_fmt;
121
7079
        par->channel_layout   = codec->channel_layout;
122
7079
        par->channels         = codec->channels;
123
7079
        par->sample_rate      = codec->sample_rate;
124
7079
        par->block_align      = codec->block_align;
125
7079
        par->frame_size       = codec->frame_size;
126
7079
        par->initial_padding  = codec->initial_padding;
127
7079
        par->trailing_padding = codec->trailing_padding;
128
7079
        par->seek_preroll     = codec->seek_preroll;
129
7079
        break;
130
262
    case AVMEDIA_TYPE_SUBTITLE:
131
262
        par->width  = codec->width;
132
262
        par->height = codec->height;
133
262
        break;
134
    }
135
136
32262
    if (codec->extradata) {
137
5067
        par->extradata = av_mallocz(codec->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
138
5067
        if (!par->extradata)
139
            return AVERROR(ENOMEM);
140
5067
        memcpy(par->extradata, codec->extradata, codec->extradata_size);
141
5067
        par->extradata_size = codec->extradata_size;
142
    }
143
144
32262
    return 0;
145
}
146
147
47629
int avcodec_parameters_to_context(AVCodecContext *codec,
148
                                  const AVCodecParameters *par)
149
{
150
47629
    codec->codec_type = par->codec_type;
151
47629
    codec->codec_id   = par->codec_id;
152
47629
    codec->codec_tag  = par->codec_tag;
153
154
47629
    codec->bit_rate              = par->bit_rate;
155
47629
    codec->bits_per_coded_sample = par->bits_per_coded_sample;
156
47629
    codec->bits_per_raw_sample   = par->bits_per_raw_sample;
157
47629
    codec->profile               = par->profile;
158
47629
    codec->level                 = par->level;
159
160

47629
    switch (par->codec_type) {
161
35578
    case AVMEDIA_TYPE_VIDEO:
162
35578
        codec->pix_fmt                = par->format;
163
35578
        codec->width                  = par->width;
164
35578
        codec->height                 = par->height;
165
35578
        codec->field_order            = par->field_order;
166
35578
        codec->color_range            = par->color_range;
167
35578
        codec->color_primaries        = par->color_primaries;
168
35578
        codec->color_trc              = par->color_trc;
169
35578
        codec->colorspace             = par->color_space;
170
35578
        codec->chroma_sample_location = par->chroma_location;
171
35578
        codec->sample_aspect_ratio    = par->sample_aspect_ratio;
172
35578
        codec->has_b_frames           = par->video_delay;
173
35578
        break;
174
11256
    case AVMEDIA_TYPE_AUDIO:
175
11256
        codec->sample_fmt       = par->format;
176
11256
        codec->channel_layout   = par->channel_layout;
177
11256
        codec->channels         = par->channels;
178
11256
        codec->sample_rate      = par->sample_rate;
179
11256
        codec->block_align      = par->block_align;
180
11256
        codec->frame_size       = par->frame_size;
181
11256
        codec->delay            =
182
11256
        codec->initial_padding  = par->initial_padding;
183
11256
        codec->trailing_padding = par->trailing_padding;
184
11256
        codec->seek_preroll     = par->seek_preroll;
185
11256
        break;
186
426
    case AVMEDIA_TYPE_SUBTITLE:
187
426
        codec->width  = par->width;
188
426
        codec->height = par->height;
189
426
        break;
190
    }
191
192
47629
    if (par->extradata) {
193
7179
        av_freep(&codec->extradata);
194
7179
        codec->extradata = av_mallocz(par->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
195
7179
        if (!codec->extradata)
196
            return AVERROR(ENOMEM);
197
7179
        memcpy(codec->extradata, par->extradata, par->extradata_size);
198
7179
        codec->extradata_size = par->extradata_size;
199
    }
200
201
47629
    return 0;
202
}