| 127 | } |
| 128 | |
| 129 | static int open_output_file(const char *filename) |
| 130 | { |
| 131 | AVStream *out_stream; |
| 132 | AVStream *in_stream; |
| 133 | AVCodecContext *dec_ctx, *enc_ctx; |
| 134 | const AVCodec *encoder; |
| 135 | int ret; |
| 136 | unsigned int i; |
| 137 | |
| 138 | ofmt_ctx = NULL; |
| 139 | avformat_alloc_output_context2(&ofmt_ctx, NULL, NULL, filename); |
| 140 | if (!ofmt_ctx) { |
| 141 | av_log(NULL, AV_LOG_ERROR, "Could not create output context\n"); |
| 142 | return AVERROR_UNKNOWN; |
| 143 | } |
| 144 | |
| 145 | |
| 146 | for (i = 0; i < ifmt_ctx->nb_streams; i++) { |
| 147 | out_stream = avformat_new_stream(ofmt_ctx, NULL); |
| 148 | if (!out_stream) { |
| 149 | av_log(NULL, AV_LOG_ERROR, "Failed allocating output stream\n"); |
| 150 | return AVERROR_UNKNOWN; |
| 151 | } |
| 152 | |
| 153 | in_stream = ifmt_ctx->streams[i]; |
| 154 | dec_ctx = stream_ctx[i].dec_ctx; |
| 155 | |
| 156 | if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO |
| 157 | || dec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) { |
| 158 | /* in this example, we choose transcoding to same codec */ |
| 159 | encoder = avcodec_find_encoder(dec_ctx->codec_id); |
| 160 | if (!encoder) { |
| 161 | av_log(NULL, AV_LOG_FATAL, "Necessary encoder not found\n"); |
| 162 | return AVERROR_INVALIDDATA; |
| 163 | } |
| 164 | enc_ctx = avcodec_alloc_context3(encoder); |
| 165 | if (!enc_ctx) { |
| 166 | av_log(NULL, AV_LOG_FATAL, "Failed to allocate the encoder context\n"); |
| 167 | return AVERROR(ENOMEM); |
| 168 | } |
| 169 | |
| 170 | /* In this example, we transcode to same properties (picture size, |
| 171 | * sample rate etc.). These properties can be changed for output |
| 172 | * streams easily using filters */ |
| 173 | if (dec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) { |
| 174 | const enum AVPixelFormat *pix_fmts = NULL; |
| 175 | |
| 176 | enc_ctx->height = dec_ctx->height; |
| 177 | enc_ctx->width = dec_ctx->width; |
| 178 | enc_ctx->sample_aspect_ratio = dec_ctx->sample_aspect_ratio; |
| 179 | |
| 180 | ret = avcodec_get_supported_config(dec_ctx, NULL, |
| 181 | AV_CODEC_CONFIG_PIX_FORMAT, 0, |
| 182 | (const void**)&pix_fmts, NULL); |
| 183 | |
| 184 | /* take first format from list of supported formats */ |
| 185 | enc_ctx->pix_fmt = (ret >= 0 && pix_fmts) ? |
| 186 | pix_fmts[0] : dec_ctx->pix_fmt; |
no test coverage detected