| 118 | } |
| 119 | |
| 120 | int ds_open(DecodeContext *dc, const char *url, int stream_idx) |
| 121 | { |
| 122 | const AVCodec *codec; |
| 123 | int ret; |
| 124 | |
| 125 | memset(dc, 0, sizeof(*dc)); |
| 126 | |
| 127 | dc->pkt = av_packet_alloc(); |
| 128 | dc->frame = av_frame_alloc(); |
| 129 | if (!dc->pkt || !dc->frame) { |
| 130 | ret = AVERROR(ENOMEM); |
| 131 | goto fail; |
| 132 | } |
| 133 | |
| 134 | ret = avformat_open_input(&dc->demuxer, url, NULL, NULL); |
| 135 | if (ret < 0) { |
| 136 | fprintf(stderr, "Error opening input file: %d\n", ret); |
| 137 | return ret; |
| 138 | } |
| 139 | |
| 140 | if (stream_idx < 0 || stream_idx >= dc->demuxer->nb_streams) |
| 141 | return AVERROR(EINVAL); |
| 142 | |
| 143 | dc->stream = dc->demuxer->streams[stream_idx]; |
| 144 | |
| 145 | codec = avcodec_find_decoder(dc->stream->codecpar->codec_id); |
| 146 | if (!codec) |
| 147 | return AVERROR_DECODER_NOT_FOUND; |
| 148 | |
| 149 | dc->decoder = avcodec_alloc_context3(codec); |
| 150 | if (!dc->decoder) |
| 151 | return AVERROR(ENOMEM); |
| 152 | |
| 153 | ret = avcodec_parameters_to_context(dc->decoder, dc->stream->codecpar); |
| 154 | if (ret < 0) |
| 155 | goto fail; |
| 156 | |
| 157 | return 0; |
| 158 | |
| 159 | fail: |
| 160 | ds_free(dc); |
| 161 | return ret; |
| 162 | } |