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 | #include "packet.h" | ||
31 | |||
32 | 265537 | static void codec_parameters_reset(AVCodecParameters *par) | |
33 | { | ||
34 | 265537 | av_freep(&par->extradata); | |
35 | 265537 | av_channel_layout_uninit(&par->ch_layout); | |
36 | 265537 | av_packet_side_data_free(&par->coded_side_data, &par->nb_coded_side_data); | |
37 | |||
38 | 265537 | memset(par, 0, sizeof(*par)); | |
39 | |||
40 | 265537 | par->codec_type = AVMEDIA_TYPE_UNKNOWN; | |
41 | 265537 | par->codec_id = AV_CODEC_ID_NONE; | |
42 | 265537 | par->format = -1; | |
43 | 265537 | par->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC; | |
44 | 265537 | par->field_order = AV_FIELD_UNKNOWN; | |
45 | 265537 | par->color_range = AVCOL_RANGE_UNSPECIFIED; | |
46 | 265537 | par->color_primaries = AVCOL_PRI_UNSPECIFIED; | |
47 | 265537 | par->color_trc = AVCOL_TRC_UNSPECIFIED; | |
48 | 265537 | par->color_space = AVCOL_SPC_UNSPECIFIED; | |
49 | 265537 | par->chroma_location = AVCHROMA_LOC_UNSPECIFIED; | |
50 | 265537 | par->sample_aspect_ratio = (AVRational){ 0, 1 }; | |
51 | 265537 | par->framerate = (AVRational){ 0, 1 }; | |
52 | 265537 | par->profile = AV_PROFILE_UNKNOWN; | |
53 | 265537 | par->level = AV_LEVEL_UNKNOWN; | |
54 | 265537 | par->alpha_mode = AVALPHA_MODE_UNSPECIFIED; | |
55 | 265537 | } | |
56 | |||
57 | 89403 | AVCodecParameters *avcodec_parameters_alloc(void) | |
58 | { | ||
59 | 89403 | AVCodecParameters *par = av_mallocz(sizeof(*par)); | |
60 | |||
61 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 89403 times.
|
89403 | if (!par) |
62 | ✗ | return NULL; | |
63 | 89403 | codec_parameters_reset(par); | |
64 | 89403 | return par; | |
65 | } | ||
66 | |||
67 | 1977374 | void avcodec_parameters_free(AVCodecParameters **ppar) | |
68 | { | ||
69 | 1977374 | AVCodecParameters *par = *ppar; | |
70 | |||
71 |
2/2✓ Branch 0 taken 1887977 times.
✓ Branch 1 taken 89397 times.
|
1977374 | if (!par) |
72 | 1887977 | return; | |
73 | 89397 | codec_parameters_reset(par); | |
74 | |||
75 | 89397 | av_freep(ppar); | |
76 | } | ||
77 | |||
78 | 151425 | static int codec_parameters_copy_side_data(AVPacketSideData **pdst, int *pnb_dst, | |
79 | const AVPacketSideData *src, int nb_src) | ||
80 | { | ||
81 | AVPacketSideData *dst; | ||
82 | 151425 | int nb_dst = *pnb_dst; | |
83 | |||
84 |
2/2✓ Branch 0 taken 143482 times.
✓ Branch 1 taken 7943 times.
|
151425 | if (!src) |
85 | 143482 | return 0; | |
86 | |||
87 | 7943 | *pdst = dst = av_calloc(nb_src, sizeof(*dst)); | |
88 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7943 times.
|
7943 | if (!dst) |
89 | ✗ | return AVERROR(ENOMEM); | |
90 | |||
91 |
2/2✓ Branch 0 taken 8495 times.
✓ Branch 1 taken 7943 times.
|
16438 | for (int i = 0; i < nb_src; i++) { |
92 | 8495 | const AVPacketSideData *src_sd = &src[i]; | |
93 | 8495 | AVPacketSideData *dst_sd = &dst[i]; | |
94 | |||
95 | 8495 | dst_sd->data = av_memdup(src_sd->data, src_sd->size); | |
96 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8495 times.
|
8495 | if (!dst_sd->data) |
97 | ✗ | return AVERROR(ENOMEM); | |
98 | |||
99 | 8495 | dst_sd->type = src_sd->type; | |
100 | 8495 | dst_sd->size = src_sd->size; | |
101 | 8495 | *pnb_dst = ++nb_dst; | |
102 | } | ||
103 | |||
104 | 7943 | return 0; | |
105 | } | ||
106 | |||
107 | 43373 | int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src) | |
108 | { | ||
109 | int ret; | ||
110 | |||
111 | 43373 | codec_parameters_reset(dst); | |
112 | 43373 | memcpy(dst, src, sizeof(*dst)); | |
113 | |||
114 | 43373 | dst->ch_layout = (AVChannelLayout){0}; | |
115 | 43373 | dst->extradata = NULL; | |
116 | 43373 | dst->extradata_size = 0; | |
117 | 43373 | dst->coded_side_data = NULL; | |
118 | 43373 | dst->nb_coded_side_data = 0; | |
119 |
2/2✓ Branch 0 taken 6511 times.
✓ Branch 1 taken 36862 times.
|
43373 | if (src->extradata) { |
120 | 6511 | dst->extradata = av_mallocz(src->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE); | |
121 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6511 times.
|
6511 | if (!dst->extradata) |
122 | ✗ | return AVERROR(ENOMEM); | |
123 | 6511 | memcpy(dst->extradata, src->extradata, src->extradata_size); | |
124 | 6511 | dst->extradata_size = src->extradata_size; | |
125 | } | ||
126 | 43373 | ret = codec_parameters_copy_side_data(&dst->coded_side_data, &dst->nb_coded_side_data, | |
127 | 43373 | src->coded_side_data, src->nb_coded_side_data); | |
128 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 43373 times.
|
43373 | if (ret < 0) |
129 | ✗ | return ret; | |
130 | |||
131 | 43373 | ret = av_channel_layout_copy(&dst->ch_layout, &src->ch_layout); | |
132 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 43373 times.
|
43373 | if (ret < 0) |
133 | ✗ | return ret; | |
134 | |||
135 | 43373 | return 0; | |
136 | } | ||
137 | |||
138 | 43364 | int avcodec_parameters_from_context(AVCodecParameters *par, | |
139 | const AVCodecContext *codec) | ||
140 | { | ||
141 | int ret; | ||
142 | |||
143 | 43364 | codec_parameters_reset(par); | |
144 | |||
145 | 43364 | par->codec_type = codec->codec_type; | |
146 | 43364 | par->codec_id = codec->codec_id; | |
147 | 43364 | par->codec_tag = codec->codec_tag; | |
148 | |||
149 | 43364 | par->bit_rate = codec->bit_rate; | |
150 | 43364 | par->bits_per_coded_sample = codec->bits_per_coded_sample; | |
151 | 43364 | par->bits_per_raw_sample = codec->bits_per_raw_sample; | |
152 | 43364 | par->profile = codec->profile; | |
153 | 43364 | par->level = codec->level; | |
154 | |||
155 |
4/4✓ Branch 0 taken 33888 times.
✓ Branch 1 taken 8974 times.
✓ Branch 2 taken 412 times.
✓ Branch 3 taken 90 times.
|
43364 | switch (par->codec_type) { |
156 | 33888 | case AVMEDIA_TYPE_VIDEO: | |
157 | 33888 | par->format = codec->pix_fmt; | |
158 | 33888 | par->width = codec->width; | |
159 | 33888 | par->height = codec->height; | |
160 | 33888 | par->field_order = codec->field_order; | |
161 | 33888 | par->color_range = codec->color_range; | |
162 | 33888 | par->color_primaries = codec->color_primaries; | |
163 | 33888 | par->color_trc = codec->color_trc; | |
164 | 33888 | par->color_space = codec->colorspace; | |
165 | 33888 | par->chroma_location = codec->chroma_sample_location; | |
166 | 33888 | par->sample_aspect_ratio = codec->sample_aspect_ratio; | |
167 | 33888 | par->video_delay = codec->has_b_frames; | |
168 | 33888 | par->framerate = codec->framerate; | |
169 | 33888 | par->alpha_mode = codec->alpha_mode; | |
170 | 33888 | break; | |
171 | 8974 | case AVMEDIA_TYPE_AUDIO: | |
172 | 8974 | par->format = codec->sample_fmt; | |
173 | 8974 | ret = av_channel_layout_copy(&par->ch_layout, &codec->ch_layout); | |
174 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8974 times.
|
8974 | if (ret < 0) |
175 | ✗ | return ret; | |
176 | 8974 | par->sample_rate = codec->sample_rate; | |
177 | 8974 | par->block_align = codec->block_align; | |
178 | 8974 | par->frame_size = codec->frame_size; | |
179 | 8974 | par->initial_padding = codec->initial_padding; | |
180 | 8974 | par->trailing_padding = codec->trailing_padding; | |
181 | 8974 | par->seek_preroll = codec->seek_preroll; | |
182 | 8974 | break; | |
183 | 412 | case AVMEDIA_TYPE_SUBTITLE: | |
184 | 412 | par->width = codec->width; | |
185 | 412 | par->height = codec->height; | |
186 | 412 | break; | |
187 | } | ||
188 | |||
189 |
2/2✓ Branch 0 taken 7197 times.
✓ Branch 1 taken 36167 times.
|
43364 | if (codec->extradata) { |
190 | 7197 | par->extradata = av_mallocz(codec->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE); | |
191 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7197 times.
|
7197 | if (!par->extradata) |
192 | ✗ | return AVERROR(ENOMEM); | |
193 | 7197 | memcpy(par->extradata, codec->extradata, codec->extradata_size); | |
194 | 7197 | par->extradata_size = codec->extradata_size; | |
195 | } | ||
196 | |||
197 | 43364 | ret = codec_parameters_copy_side_data(&par->coded_side_data, &par->nb_coded_side_data, | |
198 | 43364 | codec->coded_side_data, codec->nb_coded_side_data); | |
199 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 43364 times.
|
43364 | if (ret < 0) |
200 | ✗ | return ret; | |
201 | |||
202 | 43364 | return 0; | |
203 | } | ||
204 | |||
205 | 64688 | int avcodec_parameters_to_context(AVCodecContext *codec, | |
206 | const AVCodecParameters *par) | ||
207 | { | ||
208 | int ret; | ||
209 | |||
210 | 64688 | codec->codec_type = par->codec_type; | |
211 | 64688 | codec->codec_id = par->codec_id; | |
212 | 64688 | codec->codec_tag = par->codec_tag; | |
213 | |||
214 | 64688 | codec->bit_rate = par->bit_rate; | |
215 | 64688 | codec->bits_per_coded_sample = par->bits_per_coded_sample; | |
216 | 64688 | codec->bits_per_raw_sample = par->bits_per_raw_sample; | |
217 | 64688 | codec->profile = par->profile; | |
218 | 64688 | codec->level = par->level; | |
219 | |||
220 |
4/4✓ Branch 0 taken 52056 times.
✓ Branch 1 taken 11796 times.
✓ Branch 2 taken 560 times.
✓ Branch 3 taken 276 times.
|
64688 | switch (par->codec_type) { |
221 | 52056 | case AVMEDIA_TYPE_VIDEO: | |
222 | 52056 | codec->pix_fmt = par->format; | |
223 | 52056 | codec->width = par->width; | |
224 | 52056 | codec->height = par->height; | |
225 | 52056 | codec->field_order = par->field_order; | |
226 | 52056 | codec->color_range = par->color_range; | |
227 | 52056 | codec->color_primaries = par->color_primaries; | |
228 | 52056 | codec->color_trc = par->color_trc; | |
229 | 52056 | codec->colorspace = par->color_space; | |
230 | 52056 | codec->chroma_sample_location = par->chroma_location; | |
231 | 52056 | codec->sample_aspect_ratio = par->sample_aspect_ratio; | |
232 | 52056 | codec->has_b_frames = par->video_delay; | |
233 | 52056 | codec->framerate = par->framerate; | |
234 | 52056 | codec->alpha_mode = par->alpha_mode; | |
235 | 52056 | break; | |
236 | 11796 | case AVMEDIA_TYPE_AUDIO: | |
237 | 11796 | codec->sample_fmt = par->format; | |
238 | 11796 | ret = av_channel_layout_copy(&codec->ch_layout, &par->ch_layout); | |
239 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 11796 times.
|
11796 | if (ret < 0) |
240 | ✗ | return ret; | |
241 | 11796 | codec->sample_rate = par->sample_rate; | |
242 | 11796 | codec->block_align = par->block_align; | |
243 | 11796 | codec->frame_size = par->frame_size; | |
244 | 11796 | codec->delay = | |
245 | 11796 | codec->initial_padding = par->initial_padding; | |
246 | 11796 | codec->trailing_padding = par->trailing_padding; | |
247 | 11796 | codec->seek_preroll = par->seek_preroll; | |
248 | 11796 | break; | |
249 | 560 | case AVMEDIA_TYPE_SUBTITLE: | |
250 | 560 | codec->width = par->width; | |
251 | 560 | codec->height = par->height; | |
252 | 560 | break; | |
253 | } | ||
254 | |||
255 | 64688 | av_freep(&codec->extradata); | |
256 | 64688 | codec->extradata_size = 0; | |
257 |
2/2✓ Branch 0 taken 8861 times.
✓ Branch 1 taken 55827 times.
|
64688 | if (par->extradata) { |
258 | 8861 | codec->extradata = av_mallocz(par->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE); | |
259 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 8861 times.
|
8861 | if (!codec->extradata) |
260 | ✗ | return AVERROR(ENOMEM); | |
261 | 8861 | memcpy(codec->extradata, par->extradata, par->extradata_size); | |
262 | 8861 | codec->extradata_size = par->extradata_size; | |
263 | } | ||
264 | |||
265 | 64688 | av_packet_side_data_free(&codec->coded_side_data, &codec->nb_coded_side_data); | |
266 | 64688 | ret = codec_parameters_copy_side_data(&codec->coded_side_data, &codec->nb_coded_side_data, | |
267 | 64688 | par->coded_side_data, par->nb_coded_side_data); | |
268 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 64688 times.
|
64688 | if (ret < 0) |
269 | ✗ | return ret; | |
270 | |||
271 | 64688 | return 0; | |
272 | } | ||
273 |