History

avformat: refactor avformat_write_header

Mostly cosmetic changes adding some intermediate to shorten the lines.

Luca Barbato committed 14 years ago

Commit 86bbdf81 parent 55f90371 file · +42 −27Browse files

Unified · Split

libavformat/mux.c+42−27
@@ -140,88 +140,103 @@ int avformat_write_header(AVFormatContext *s, AVDictionary **options) · 139 unmodified lines
140 int ret = 0, i;140 int ret = 0, i;
141 AVStream *st;141 AVStream *st;
142 AVDictionary *tmp = NULL;142 AVDictionary *tmp = NULL;
143 AVCodecContext *codec = NULL;
144 AVOutputFormat *of = s->oformat;
143 145
144 if (options)146 if (options)
145 av_dict_copy(&tmp, *options, 0);147 av_dict_copy(&tmp, *options, 0);
148
146 if ((ret = av_opt_set_dict(s, &tmp)) < 0)149 if ((ret = av_opt_set_dict(s, &tmp)) < 0)
147 goto fail;150 goto fail;
148 151
149 // some sanity checks152 // some sanity checks
150 if (s->nb_streams == 0 && !(s->oformat->flags & AVFMT_NOSTREAMS)) {153 if (s->nb_streams == 0 && !(of->flags & AVFMT_NOSTREAMS)) {
151 av_log(s, AV_LOG_ERROR, "no streams\n");154 av_log(s, AV_LOG_ERROR, "no streams\n");
152 ret = AVERROR(EINVAL);155 ret = AVERROR(EINVAL);
153 goto fail;156 goto fail;
154 }157 }
155 158
156 for (i = 0; i < s->nb_streams; i++) {159 for (i = 0; i < s->nb_streams; i++) {
157 st = s->streams[i];160 st = s->streams[i];
161 codec = st->codec;
158 162
159 switch (st->codec->codec_type) {163 switch (codec->codec_type) {
160 case AVMEDIA_TYPE_AUDIO:164 case AVMEDIA_TYPE_AUDIO:
161 if (st->codec->sample_rate <= 0) {165 if (codec->sample_rate <= 0) {
162 av_log(s, AV_LOG_ERROR, "sample rate not set\n");166 av_log(s, AV_LOG_ERROR, "sample rate not set\n");
163 ret = AVERROR(EINVAL);167 ret = AVERROR(EINVAL);
164 goto fail;168 goto fail;
165 }169 }
166 if (!st->codec->block_align)170 if (!codec->block_align)
167 st->codec->block_align = st->codec->channels *171 codec->block_align = codec->channels *
168 av_get_bits_per_sample(st->codec->codec_id) >> 3;172 av_get_bits_per_sample(codec->codec_id) >> 3;
169 break;173 break;
170 case AVMEDIA_TYPE_VIDEO:174 case AVMEDIA_TYPE_VIDEO:
171 if (st->codec->time_base.num <= 0 || st->codec->time_base.den <= 0) { //FIXME audio too?175 if (codec->time_base.num <= 0 ||
176 codec->time_base.den <= 0) { //FIXME audio too?
172 av_log(s, AV_LOG_ERROR, "time base not set\n");177 av_log(s, AV_LOG_ERROR, "time base not set\n");
173 ret = AVERROR(EINVAL);178 ret = AVERROR(EINVAL);
174 goto fail;179 goto fail;
175 }180 }
176 if ((st->codec->width <= 0 || st->codec->height <= 0) && !(s->oformat->flags & AVFMT_NODIMENSIONS)) {181
182 if ((codec->width <= 0 || codec->height <= 0) &&
183 !(of->flags & AVFMT_NODIMENSIONS)) {
177 av_log(s, AV_LOG_ERROR, "dimensions not set\n");184 av_log(s, AV_LOG_ERROR, "dimensions not set\n");
178 ret = AVERROR(EINVAL);185 ret = AVERROR(EINVAL);
179 goto fail;186 goto fail;
180 }187 }
181 if (av_cmp_q(st->sample_aspect_ratio, st->codec->sample_aspect_ratio)) {188
189 if (av_cmp_q(st->sample_aspect_ratio,
190 codec->sample_aspect_ratio)) {
182 av_log(s, AV_LOG_ERROR, "Aspect ratio mismatch between muxer "191 av_log(s, AV_LOG_ERROR, "Aspect ratio mismatch between muxer "
183 "(%d/%d) and encoder layer (%d/%d)\n",192 "(%d/%d) and encoder layer (%d/%d)\n",
184 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,193 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
185 st->codec->sample_aspect_ratio.num,194 codec->sample_aspect_ratio.num,
186 st->codec->sample_aspect_ratio.den);195 codec->sample_aspect_ratio.den);
187 ret = AVERROR(EINVAL);196 ret = AVERROR(EINVAL);
188 goto fail;197 goto fail;
189 }198 }
190 break;199 break;
191 }200 }
192 201
193 if (s->oformat->codec_tag) {202 if (of->codec_tag) {
194 if (st->codec->codec_tag && st->codec->codec_id == AV_CODEC_ID_RAWVIDEO && av_codec_get_tag(s->oformat->codec_tag, st->codec->codec_id) == 0 && !validate_codec_tag(s, st)) {203 if (codec->codec_tag &&
195 //the current rawvideo encoding system ends up setting the wrong codec_tag for avi, we override it here204 codec->codec_id == AV_CODEC_ID_RAWVIDEO &&
196 st->codec->codec_tag = 0;205 !av_codec_get_tag(of->codec_tag, codec->codec_id) &&
206 !validate_codec_tag(s, st)) {
207 // the current rawvideo encoding system ends up setting
208 // the wrong codec_tag for avi, we override it here
209 codec->codec_tag = 0;
197 }210 }
198 if (st->codec->codec_tag) {211 if (codec->codec_tag) {
199 if (!validate_codec_tag(s, st)) {212 if (!validate_codec_tag(s, st)) {
200 char tagbuf[32];213 char tagbuf[32];
201 av_get_codec_tag_string(tagbuf, sizeof(tagbuf), st->codec->codec_tag);214 av_get_codec_tag_string(tagbuf, sizeof(tagbuf), codec->codec_tag);
202 av_log(s, AV_LOG_ERROR,215 av_log(s, AV_LOG_ERROR,
203 "Tag %s/0x%08x incompatible with output codec id '%d'\n",216 "Tag %s/0x%08x incompatible with output codec id '%d'\n",
204 tagbuf, st->codec->codec_tag, st->codec->codec_id);217 tagbuf, codec->codec_tag, codec->codec_id);
205 ret = AVERROR_INVALIDDATA;218 ret = AVERROR_INVALIDDATA;
206 goto fail;219 goto fail;
207 }220 }
208 } else221 } else
209 st->codec->codec_tag = av_codec_get_tag(s->oformat->codec_tag, st->codec->codec_id);222 codec->codec_tag = av_codec_get_tag(of->codec_tag, codec->codec_id);
210 }223 }
211 224
212 if (s->oformat->flags & AVFMT_GLOBALHEADER &&225 if (of->flags & AVFMT_GLOBALHEADER &&
213 !(st->codec->flags & CODEC_FLAG_GLOBAL_HEADER))226 !(codec->flags & CODEC_FLAG_GLOBAL_HEADER))
214 av_log(s, AV_LOG_WARNING, "Codec for stream %d does not use global headers but container format requires global headers\n", i);227 av_log(s, AV_LOG_WARNING,
228 "Codec for stream %d does not use global headers "
229 "but container format requires global headers\n", i);
215 }230 }
216 231
217 if (!s->priv_data && s->oformat->priv_data_size > 0) {232 if (!s->priv_data && of->priv_data_size > 0) {
218 s->priv_data = av_mallocz(s->oformat->priv_data_size);233 s->priv_data = av_mallocz(of->priv_data_size);
219 if (!s->priv_data) {234 if (!s->priv_data) {
220 ret = AVERROR(ENOMEM);235 ret = AVERROR(ENOMEM);
221 goto fail;236 goto fail;
222 }237 }
223 if (s->oformat->priv_class) {238 if (of->priv_class) {
224 *(const AVClass **)s->priv_data = s->oformat->priv_class;239 *(const AVClass **)s->priv_data = of->priv_class;
225 av_opt_set_defaults(s->priv_data);240 av_opt_set_defaults(s->priv_data);
226 if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)241 if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
227 goto fail;242 goto fail;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t