// Open video file if (avformat_open_input(&pFormatCtx, url, NULL, NULL) != 0) {
LOGD("Couldn't open url:%s\n", url); return; // Couldn't open file }
// Retrieve stream information if (avformat_find_stream_info(pFormatCtx, NULL) < 0) { LOGD("Couldn't find stream information."); return; }
// Find the first video stream int videoStream = -1, i; for (i = 0; i < pFormatCtx->nb_streams; i++) { if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && videoStream < 0) { videoStream = i; } } if (videoStream == -1) { LOGD("Didn't find a video stream."); return; // Didn't find a video stream }
if (getCodecContext(pFormatCtx->streams[videoStream]->codecpar, &pCodecCtx) != 0) { LOGD("Didn't get CodecContext."); return; } //-----------------------------AVCodecContext init end-------------------------------
/* buffer video source: the decoded frames from the decoder will be inserted here. */ snprintf(args, sizeof(args), "video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d", pCodecCtx->width, pCodecCtx->height, pCodecCtx->pix_fmt, pFormatCtx->streams[videoStream]->time_base.num, pFormatCtx->streams[videoStream]->time_base.den, pCodecCtx->sample_aspect_ratio.num, pCodecCtx->sample_aspect_ratio.den);
AVPacket *packet = av_packet_alloc(); int count = 0; while (av_read_frame(pFormatCtx, packet) == 0) { // Is this a packet from the video stream? if (packet->stream_index == videoStream) { // Decode video frame if (avcodec_send_packet(pCodecCtx, packet) != 0) { break; }