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
140140 int ret = 0, i;
141141 AVStream *st;
142142 AVDictionary *tmp = NULL;
143+ AVCodecContext *codec = NULL;
144+ AVOutputFormat *of = s->oformat;
143145
144146 if (options)
145147 av_dict_copy(&tmp, *options, 0);
148+
146149 if ((ret = av_opt_set_dict(s, &tmp)) < 0)
147150 goto fail;
148151
149152 // some sanity checks
150− if (s->nb_streams == 0 && !(s->oformat->flags & AVFMT_NOSTREAMS)) {
153+ if (s->nb_streams == 0 && !(of->flags & AVFMT_NOSTREAMS)) {
151154 av_log(s, AV_LOG_ERROR, "no streams\n");
152155 ret = AVERROR(EINVAL);
153156 goto fail;
154157 }
155158
156159 for (i = 0; i < s->nb_streams; i++) {
157− st = s->streams[i];
160+ st = s->streams[i];
161+ codec = st->codec;
158162
159− switch (st->codec->codec_type) {
163+ switch (codec->codec_type) {
160164 case AVMEDIA_TYPE_AUDIO:
161− if (st->codec->sample_rate <= 0) {
165+ if (codec->sample_rate <= 0) {
162166 av_log(s, AV_LOG_ERROR, "sample rate not set\n");
163167 ret = AVERROR(EINVAL);
164168 goto fail;
165169 }
166− if (!st->codec->block_align)
167− st->codec->block_align = st->codec->channels *
168− av_get_bits_per_sample(st->codec->codec_id) >> 3;
170+ if (!codec->block_align)
171+ codec->block_align = codec->channels *
172+ av_get_bits_per_sample(codec->codec_id) >> 3;
169173 break;
170174 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?
172177 av_log(s, AV_LOG_ERROR, "time base not set\n");
173178 ret = AVERROR(EINVAL);
174179 goto fail;
175180 }
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)) {
177184 av_log(s, AV_LOG_ERROR, "dimensions not set\n");
178185 ret = AVERROR(EINVAL);
179186 goto fail;
180187 }
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)) {
182191 av_log(s, AV_LOG_ERROR, "Aspect ratio mismatch between muxer "
183192 "(%d/%d) and encoder layer (%d/%d)\n",
184193 st->sample_aspect_ratio.num, st->sample_aspect_ratio.den,
185− st->codec->sample_aspect_ratio.num,
186− st->codec->sample_aspect_ratio.den);
194+ codec->sample_aspect_ratio.num,
195+ codec->sample_aspect_ratio.den);
187196 ret = AVERROR(EINVAL);
188197 goto fail;
189198 }
190199 break;
191200 }
192201
193− if (s->oformat->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)) {
195− //the current rawvideo encoding system ends up setting the wrong codec_tag for avi, we override it here
196− st->codec->codec_tag = 0;
202+ if (of->codec_tag) {
203+ if (codec->codec_tag &&
204+ codec->codec_id == AV_CODEC_ID_RAWVIDEO &&
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;
197210 }
198− if (st->codec->codec_tag) {
211+ if (codec->codec_tag) {
199212 if (!validate_codec_tag(s, st)) {
200213 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);
202215 av_log(s, AV_LOG_ERROR,
203216 "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);
205218 ret = AVERROR_INVALIDDATA;
206219 goto fail;
207220 }
208221 } 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);
210223 }
211224
212− if (s->oformat->flags & AVFMT_GLOBALHEADER &&
213− !(st->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);
225+ if (of->flags & AVFMT_GLOBALHEADER &&
226+ !(codec->flags & CODEC_FLAG_GLOBAL_HEADER))
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);
215230 }
216231
217− if (!s->priv_data && s->oformat->priv_data_size > 0) {
218− s->priv_data = av_mallocz(s->oformat->priv_data_size);
232+ if (!s->priv_data && of->priv_data_size > 0) {
233+ s->priv_data = av_mallocz(of->priv_data_size);
219234 if (!s->priv_data) {
220235 ret = AVERROR(ENOMEM);
221236 goto fail;
222237 }
223− if (s->oformat->priv_class) {
224− *(const AVClass **)s->priv_data = s->oformat->priv_class;
238+ if (of->priv_class) {
239+ *(const AVClass **)s->priv_data = of->priv_class;
225240 av_opt_set_defaults(s->priv_data);
226241 if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
227242 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