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