//创建frame, 用于存储解码后的数据 frame = av_frame_alloc(); //创建pkt,用于存储解封装后从文件中读取的音视频包 pkt = av_packet_alloc(); /* 从文件中循环读取音视频帧,存入pkt 根据类型区分音频帧还是视频帧,分别送给对应的解码器去解码 */ while (av_read_frame(fmt_ctx, pkt) >= 0) { // check if the packet belongs to a stream we are interested in, otherwise // skip it if (pkt->stream_index == video_stream_idx) ret = decode_packet(video_dec_ctx, pkt); elseif (pkt->stream_index == audio_stream_idx) ret = decode_packet(audio_dec_ctx, pkt); av_packet_unref(pkt); if (ret < 0) break; }
/* 冲洗音视频解码器,将剩余的解码数据读出来 */ if (video_dec_ctx) decode_packet(video_dec_ctx, NULL); if (audio_dec_ctx) decode_packet(audio_dec_ctx, NULL); end: //资源释放和退出 avcodec_free_context(&video_dec_ctx); avcodec_free_context(&audio_dec_ctx); avformat_close_input(&fmt_ctx); if (video_dst_file) fclose(video_dst_file); if (audio_dst_file) fclose(audio_dst_file); av_packet_free(&pkt); av_frame_free(&frame); av_free(video_dst_data[0]);
staticintdecode_packet(AVCodecContext *dec, const AVPacket *pkt) { int ret = 0;
// submit the packet to the decoder ret = avcodec_send_packet(dec, pkt); if (ret < 0) { fprintf(stderr, "Error submitting a packet for decoding (%s)\n", av_err2str(ret)); return ret; }
// get all the available frames from the decoder while (ret >= 0) { ret = avcodec_receive_frame(dec, frame); if (ret < 0) { // those two return values are special and mean there is no output // frame available, but there were no errors during decoding if (ret == AVERROR_EOF || ret == AVERROR(EAGAIN)) return0;
fprintf(stderr, "Error during decoding (%s)\n", av_err2str(ret)); return ret; }
// write the frame data to output file if (dec->codec->type == AVMEDIA_TYPE_VIDEO) ret = output_video_frame(frame); else ret = output_audio_frame(frame);
staticintoutput_video_frame(AVFrame *frame) { //解码中,发现宽高或者格式变了,报错 if (frame->width != width || frame->height != height || frame->format != pix_fmt) { /* To handle this change, one could call av_image_alloc again and * decode the following frames into another rawvideo file. */ fprintf(stderr, "Error: Width, height and pixel format have to be " "constant in a rawvideo file, but the width, height or " "pixel format of the input video changed:\n" "old: width = %d, height = %d, format = %s\n" "new: width = %d, height = %d, format = %s\n", width, height, av_get_pix_fmt_name(pix_fmt), frame->width, frame->height, av_get_pix_fmt_name(frame->format)); return-1; } printf("video_frame n:%d coded_n:%d\n", video_frame_count++, frame->coded_picture_number); /* 从frame中读取解码后的数据,拷贝到之前申请的buffer中 调用av_image_copy是为了去除frame中可能的字节对齐的数据 写入文件中的数据,要求不含有字节对齐的数据 */
/* Write the raw audio data samples of the first plane. This works * fine for packed formats (e.g. AV_SAMPLE_FMT_S16). However, * most audio decoders output planar audio, which uses a separate * plane of audio samples for each channel (e.g. AV_SAMPLE_FMT_S16P). * In other words, this code will write only the first audio channel * in these cases. * You should use libswresample or libavfilter to convert the frame * to packed data. */