FFmpeg coverage


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