| 60 | static StreamContext *stream_ctx; |
| 61 | |
| 62 | static int open_input_file(const char *filename) |
| 63 | { |
| 64 | int ret; |
| 65 | unsigned int i; |
| 66 | |
| 67 | ifmt_ctx = NULL; |
| 68 | if ((ret = avformat_open_input(&ifmt_ctx, filename, NULL, NULL)) < 0) { |
| 69 | av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n"); |
| 70 | return ret; |
| 71 | } |
| 72 | |
| 73 | if ((ret = avformat_find_stream_info(ifmt_ctx, NULL)) < 0) { |
| 74 | av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n"); |
| 75 | return ret; |
| 76 | } |
| 77 | |
| 78 | stream_ctx = av_calloc(ifmt_ctx->nb_streams, sizeof(*stream_ctx)); |
| 79 | if (!stream_ctx) |
| 80 | return AVERROR(ENOMEM); |
| 81 | |
| 82 | for (i = 0; i < ifmt_ctx->nb_streams; i++) { |
| 83 | AVStream *stream = ifmt_ctx->streams[i]; |
| 84 | const AVCodec *dec = avcodec_find_decoder(stream->codecpar->codec_id); |
| 85 | AVCodecContext *codec_ctx; |
| 86 | if (!dec) { |
| 87 | av_log(NULL, AV_LOG_ERROR, "Failed to find decoder for stream #%u\n", i); |
| 88 | return AVERROR_DECODER_NOT_FOUND; |
| 89 | } |
| 90 | codec_ctx = avcodec_alloc_context3(dec); |
| 91 | if (!codec_ctx) { |
| 92 | av_log(NULL, AV_LOG_ERROR, "Failed to allocate the decoder context for stream #%u\n", i); |
| 93 | return AVERROR(ENOMEM); |
| 94 | } |
| 95 | ret = avcodec_parameters_to_context(codec_ctx, stream->codecpar); |
| 96 | if (ret < 0) { |
| 97 | av_log(NULL, AV_LOG_ERROR, "Failed to copy decoder parameters to input decoder context " |
| 98 | "for stream #%u\n", i); |
| 99 | return ret; |
| 100 | } |
| 101 | |
| 102 | /* Inform the decoder about the timebase for the packet timestamps. |
| 103 | * This is highly recommended, but not mandatory. */ |
| 104 | codec_ctx->pkt_timebase = stream->time_base; |
| 105 | |
| 106 | /* Reencode video & audio and remux subtitles etc. */ |
| 107 | if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO |
| 108 | || codec_ctx->codec_type == AVMEDIA_TYPE_AUDIO) { |
| 109 | if (codec_ctx->codec_type == AVMEDIA_TYPE_VIDEO) |
| 110 | codec_ctx->framerate = av_guess_frame_rate(ifmt_ctx, stream, NULL); |
| 111 | /* Open decoder */ |
| 112 | ret = avcodec_open2(codec_ctx, dec, NULL); |
| 113 | if (ret < 0) { |
| 114 | av_log(NULL, AV_LOG_ERROR, "Failed to open decoder for stream #%u\n", i); |
| 115 | return ret; |
| 116 | } |
| 117 | } |
| 118 | stream_ctx[i].dec_ctx = codec_ctx; |
| 119 |
no test coverage detected