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