FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/codec_par.c
Date: 2022-01-28 07:56:06
Exec Total Coverage
Lines: 128 133 96.2%
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 149147 static void codec_parameters_reset(AVCodecParameters *par)
32 {
33 149147 av_freep(&par->extradata);
34
35 149147 memset(par, 0, sizeof(*par));
36
37 149147 par->codec_type = AVMEDIA_TYPE_UNKNOWN;
38 149147 par->codec_id = AV_CODEC_ID_NONE;
39 149147 par->format = -1;
40 149147 par->field_order = AV_FIELD_UNKNOWN;
41 149147 par->color_range = AVCOL_RANGE_UNSPECIFIED;
42 149147 par->color_primaries = AVCOL_PRI_UNSPECIFIED;
43 149147 par->color_trc = AVCOL_TRC_UNSPECIFIED;
44 149147 par->color_space = AVCOL_SPC_UNSPECIFIED;
45 149147 par->chroma_location = AVCHROMA_LOC_UNSPECIFIED;
46 149147 par->sample_aspect_ratio = (AVRational){ 0, 1 };
47 149147 par->profile = FF_PROFILE_UNKNOWN;
48 149147 par->level = FF_LEVEL_UNKNOWN;
49 149147 }
50
51 50247 AVCodecParameters *avcodec_parameters_alloc(void)
52 {
53 50247 AVCodecParameters *par = av_mallocz(sizeof(*par));
54
55
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50247 times.
50247 if (!par)
56 return NULL;
57 50247 codec_parameters_reset(par);
58 50247 return par;
59 }
60
61 50241 void avcodec_parameters_free(AVCodecParameters **ppar)
62 {
63 50241 AVCodecParameters *par = *ppar;
64
65
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 50241 times.
50241 if (!par)
66 return;
67 50241 codec_parameters_reset(par);
68
69 50241 av_freep(ppar);
70 }
71
72 15381 int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src)
73 {
74 15381 codec_parameters_reset(dst);
75 15381 memcpy(dst, src, sizeof(*dst));
76
77 15381 dst->extradata = NULL;
78 15381 dst->extradata_size = 0;
79
2/2
✓ Branch 0 taken 2391 times.
✓ Branch 1 taken 12990 times.
15381 if (src->extradata) {
80 2391 dst->extradata = av_mallocz(src->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
81
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2391 times.
2391 if (!dst->extradata)
82 return AVERROR(ENOMEM);
83 2391 memcpy(dst->extradata, src->extradata, src->extradata_size);
84 2391 dst->extradata_size = src->extradata_size;
85 }
86
87 15381 return 0;
88 }
89
90 33278 int avcodec_parameters_from_context(AVCodecParameters *par,
91 const AVCodecContext *codec)
92 {
93 33278 codec_parameters_reset(par);
94
95 33278 par->codec_type = codec->codec_type;
96 33278 par->codec_id = codec->codec_id;
97 33278 par->codec_tag = codec->codec_tag;
98
99 33278 par->bit_rate = codec->bit_rate;
100 33278 par->bits_per_coded_sample = codec->bits_per_coded_sample;
101 33278 par->bits_per_raw_sample = codec->bits_per_raw_sample;
102 33278 par->profile = codec->profile;
103 33278 par->level = codec->level;
104
105
4/4
✓ Branch 0 taken 25514 times.
✓ Branch 1 taken 7335 times.
✓ Branch 2 taken 294 times.
✓ Branch 3 taken 135 times.
33278 switch (par->codec_type) {
106 25514 case AVMEDIA_TYPE_VIDEO:
107 25514 par->format = codec->pix_fmt;
108 25514 par->width = codec->width;
109 25514 par->height = codec->height;
110 25514 par->field_order = codec->field_order;
111 25514 par->color_range = codec->color_range;
112 25514 par->color_primaries = codec->color_primaries;
113 25514 par->color_trc = codec->color_trc;
114 25514 par->color_space = codec->colorspace;
115 25514 par->chroma_location = codec->chroma_sample_location;
116 25514 par->sample_aspect_ratio = codec->sample_aspect_ratio;
117 25514 par->video_delay = codec->has_b_frames;
118 25514 break;
119 7335 case AVMEDIA_TYPE_AUDIO:
120 7335 par->format = codec->sample_fmt;
121 7335 par->channel_layout = codec->channel_layout;
122 7335 par->channels = codec->channels;
123 7335 par->sample_rate = codec->sample_rate;
124 7335 par->block_align = codec->block_align;
125 7335 par->frame_size = codec->frame_size;
126 7335 par->initial_padding = codec->initial_padding;
127 7335 par->trailing_padding = codec->trailing_padding;
128 7335 par->seek_preroll = codec->seek_preroll;
129 7335 break;
130 294 case AVMEDIA_TYPE_SUBTITLE:
131 294 par->width = codec->width;
132 294 par->height = codec->height;
133 294 break;
134 }
135
136
2/2
✓ Branch 0 taken 5232 times.
✓ Branch 1 taken 28046 times.
33278 if (codec->extradata) {
137 5232 par->extradata = av_mallocz(codec->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5232 times.
5232 if (!par->extradata)
139 return AVERROR(ENOMEM);
140 5232 memcpy(par->extradata, codec->extradata, codec->extradata_size);
141 5232 par->extradata_size = codec->extradata_size;
142 }
143
144 33278 return 0;
145 }
146
147 35071 int avcodec_parameters_to_context(AVCodecContext *codec,
148 const AVCodecParameters *par)
149 {
150 35071 codec->codec_type = par->codec_type;
151 35071 codec->codec_id = par->codec_id;
152 35071 codec->codec_tag = par->codec_tag;
153
154 35071 codec->bit_rate = par->bit_rate;
155 35071 codec->bits_per_coded_sample = par->bits_per_coded_sample;
156 35071 codec->bits_per_raw_sample = par->bits_per_raw_sample;
157 35071 codec->profile = par->profile;
158 35071 codec->level = par->level;
159
160
4/4
✓ Branch 0 taken 26070 times.
✓ Branch 1 taken 8364 times.
✓ Branch 2 taken 342 times.
✓ Branch 3 taken 295 times.
35071 switch (par->codec_type) {
161 26070 case AVMEDIA_TYPE_VIDEO:
162 26070 codec->pix_fmt = par->format;
163 26070 codec->width = par->width;
164 26070 codec->height = par->height;
165 26070 codec->field_order = par->field_order;
166 26070 codec->color_range = par->color_range;
167 26070 codec->color_primaries = par->color_primaries;
168 26070 codec->color_trc = par->color_trc;
169 26070 codec->colorspace = par->color_space;
170 26070 codec->chroma_sample_location = par->chroma_location;
171 26070 codec->sample_aspect_ratio = par->sample_aspect_ratio;
172 26070 codec->has_b_frames = par->video_delay;
173 26070 break;
174 8364 case AVMEDIA_TYPE_AUDIO:
175 8364 codec->sample_fmt = par->format;
176 8364 codec->channel_layout = par->channel_layout;
177 8364 codec->channels = par->channels;
178 8364 codec->sample_rate = par->sample_rate;
179 8364 codec->block_align = par->block_align;
180 8364 codec->frame_size = par->frame_size;
181 8364 codec->delay =
182 8364 codec->initial_padding = par->initial_padding;
183 8364 codec->trailing_padding = par->trailing_padding;
184 8364 codec->seek_preroll = par->seek_preroll;
185 8364 break;
186 342 case AVMEDIA_TYPE_SUBTITLE:
187 342 codec->width = par->width;
188 342 codec->height = par->height;
189 342 break;
190 }
191
192
2/2
✓ Branch 0 taken 5097 times.
✓ Branch 1 taken 29974 times.
35071 if (par->extradata) {
193 5097 av_freep(&codec->extradata);
194 5097 codec->extradata = av_mallocz(par->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5097 times.
5097 if (!codec->extradata)
196 return AVERROR(ENOMEM);
197 5097 memcpy(codec->extradata, par->extradata, par->extradata_size);
198 5097 codec->extradata_size = par->extradata_size;
199 }
200
201 35071 return 0;
202 }
203