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